From e28b8f3f1d0bdb0ecec81e130f18c751b91d467d Mon Sep 17 00:00:00 2001 From: Svetlozar Iliev Date: Fri, 16 Aug 2024 21:46:49 +0300 Subject: [PATCH] Expose more properties of quick pick & add workspace remove action --- history.md | 5 +++++ src/__tests__/handler/workspace.test.ts | 15 +++++++++++++++ src/handler/workspace.ts | 7 +++++++ src/model/quickpick.ts | 4 ++++ src/plugin.ts | 1 + typings/index.d.ts | 18 ++++++++++++++++++ 6 files changed, 50 insertions(+) diff --git a/history.md b/history.md index c62e79909ce..e289de7262a 100644 --- a/history.md +++ b/history.md @@ -1,3 +1,8 @@ +# 2024-08-20 + +- Add `CocAction('removeWorkspaceFolder')`. +- Expanded the quick pick API in typings + # 2024-08-12 - Added `coc.preferences.formatterExtension` configuration diff --git a/src/__tests__/handler/workspace.test.ts b/src/__tests__/handler/workspace.test.ts index 029a207a649..fa396048e4c 100644 --- a/src/__tests__/handler/workspace.test.ts +++ b/src/__tests__/handler/workspace.test.ts @@ -220,6 +220,21 @@ describe('Workspace handler', () => { expect(find).toBeDefined() }) + it('should remove workspace folder', async () => { + expect(() => { + handler.addWorkspaceFolder(__filename) + }).toThrow(Error) + expect(() => { + handler.addWorkspaceFolder(__filename) + }).toThrow(Error) + await helper.plugin.cocAction('addWorkspaceFolder', __dirname) + await helper.plugin.cocAction('removeWorkspaceFolder', __dirname) + let folders = workspace.workspaceFolderControl.workspaceFolders + let uri = URI.file(__dirname).toString() + let find = folders.find(o => o.uri === uri) + expect(find).toBeUndefined() + }) + it('should check env on vim resized', async () => { await events.fire('VimResized', [80, 80]) expect(workspace.env.columns).toBe(80) diff --git a/src/handler/workspace.ts b/src/handler/workspace.ts index 8fbf4c84de7..405cfecdaa1 100644 --- a/src/handler/workspace.ts +++ b/src/handler/workspace.ts @@ -174,6 +174,13 @@ export default class WorkspaceHandler { workspace.workspaceFolderControl.addWorkspaceFolder(folder, true) } + public removeWorkspaceFolder(folder: string): void { + if (!Is.string(folder)) throw TypeError(`folder should be string`) + folder = workspace.expand(folder) + if (!isDirectory(folder)) throw directoryNotExists(folder) + workspace.workspaceFolderControl.removeWorkspaceFolder(folder) + } + public async bufferCheck(): Promise { let doc = await workspace.document if (!doc.attached) { diff --git a/src/model/quickpick.ts b/src/model/quickpick.ts index 6fc9370f2cc..dcd3db9baea 100644 --- a/src/model/quickpick.ts +++ b/src/model/quickpick.ts @@ -118,6 +118,10 @@ export default class QuickPick { return this.win?.winid } + public get inputBox(): InputBox | undefined { + return this.input + } + public setCursor(index: number): void { this.win?.setCursor(index, true) } diff --git a/src/plugin.ts b/src/plugin.ts index f7b1860b2fe..57cca98d6ee 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -52,6 +52,7 @@ export default class Plugin { this.addAction('rootPatterns', (bufnr: number) => this.handler.workspace.getRootPatterns(bufnr)) this.addAction('ensureDocument', () => this.handler.workspace.ensureDocument()) this.addAction('addWorkspaceFolder', (folder: string) => this.handler.workspace.addWorkspaceFolder(folder)) + this.addAction('removeWorkspaceFolder', (folder: string) => this.handler.workspace.removeWorkspaceFolder(folder)) this.addAction('getConfig', (key: string) => this.handler.workspace.getConfiguration(key)) this.addAction('doAutocmd', (id: number, ...args: []) => this.handler.workspace.doAutocmd(id, args)) this.addAction('openLog', () => this.handler.workspace.openLog()) diff --git a/typings/index.d.ts b/typings/index.d.ts index 908fd8d8591..81a37bc8adf 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -9554,6 +9554,24 @@ declare module 'coc.nvim' { * Undefined by default, which means the width is dynamically calculated. */ width: number | undefined + /** + * Represents the input prompt box field of the quickpick element + **/ + readonly inputBox: InputBox | undefined + /** + * The current selection index, can be used to act on an item with onDidFinish, even + * if the item is not selected. The index corresponds to the .items or .activeItems + * arrays, and can be used to index into them + **/ + readonly currIndex: number + /** + * The buffer for the popup element of the quick pick containing the .items to be selected + **/ + readonly buffer: number + /** + * The window for the popup element of the quick pick containing the .items to be selected + **/ + readonly winid: number | undefined /** * An event signaling when QuickPick closed, fired with selected items or null when canceled. */