From f89a3d011786681add092b152538592d1c91a591 Mon Sep 17 00:00:00 2001 From: Vitaly Date: Wed, 4 Oct 2023 20:07:38 +0300 Subject: [PATCH 1/4] 1 --- src/onEnterActions.ts | 3 +++ typescript/src/ipcTypes.ts | 1 + typescript/src/specialCommands/handle.ts | 3 +++ 3 files changed, 7 insertions(+) create mode 100644 src/onEnterActions.ts diff --git a/src/onEnterActions.ts b/src/onEnterActions.ts new file mode 100644 index 0000000..968f45a --- /dev/null +++ b/src/onEnterActions.ts @@ -0,0 +1,3 @@ +export default () => { + // todo add +} diff --git a/typescript/src/ipcTypes.ts b/typescript/src/ipcTypes.ts index c9b713c..ff40c09 100644 --- a/typescript/src/ipcTypes.ts +++ b/typescript/src/ipcTypes.ts @@ -16,6 +16,7 @@ export const triggerCharacterCommands = [ 'getExtendedCodeActionEdits', 'getLastResolvedCompletion', 'getArgumentReferencesFromCurrentParameter', + 'onEnterActions', ] as const export type TriggerCharacterCommand = (typeof triggerCharacterCommands)[number] diff --git a/typescript/src/specialCommands/handle.ts b/typescript/src/specialCommands/handle.ts index 80e3d0e..918d4c4 100644 --- a/typescript/src/specialCommands/handle.ts +++ b/typescript/src/specialCommands/handle.ts @@ -278,6 +278,9 @@ export default ( ) } + if (specialCommand === 'onEnterActions') { + } + return null } From 35e3152b8030542d4e8a9292434fedad6ffda785 Mon Sep 17 00:00:00 2001 From: Vitaly Turovsky Date: Fri, 27 Oct 2023 23:02:56 +0300 Subject: [PATCH 2/4] remove integration testing as we have unit testing --- .github/workflows/ci.yml | 3 --- CONTRIBUTING.MD | 2 ++ 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index cff7cd3..96a7e9a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -33,9 +33,6 @@ jobs: # if command with timeout already failed on unix, Windows job will be cancelled - run: pnpm test-plugin if: ${{ runner.os == 'Windows' }} - - uses: GabrielBB/xvfb-action@v1.6 - with: - run: pnpm integration-test - run: cd out && npx @vscode/vsce package --out ../extension.vsix - name: Archive production artifacts uses: actions/upload-artifact@v3 diff --git a/CONTRIBUTING.MD b/CONTRIBUTING.MD index 52ead98..a803e92 100644 --- a/CONTRIBUTING.MD +++ b/CONTRIBUTING.MD @@ -31,6 +31,8 @@ To start the VS Code plugin extension locally for developing: #### Unit Tests +> Note: currently project doesn't use integration testing so you can ignore `integration` folder + They are in `typescript/test` and using vitest, so they faster than integration. Feel free to add new tests here. But note that most of tests are completion tests, but I do hope to add more types tests in the future. To launch them run `pnpm test-plugin`. From 654477becf243258b6b597ee47abccee1ed8bd6e Mon Sep 17 00:00:00 2001 From: Vitaly Turovsky Date: Fri, 27 Oct 2023 23:05:32 +0300 Subject: [PATCH 3/4] wip2 --- src/onEnterActions.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/onEnterActions.ts b/src/onEnterActions.ts index 968f45a..77561c5 100644 --- a/src/onEnterActions.ts +++ b/src/onEnterActions.ts @@ -1,3 +1,15 @@ +import { defaultJsSupersetLangsWithVue } from '@zardoy/vscode-utils/build/langs' +import * as vscode from 'vscode' export default () => { - // todo add + vscode.workspace.onDidChangeTextDocument(({ contentChanges, document, reason }) => { + if ( + !contentChanges.length || + !vscode.languages.match(defaultJsSupersetLangsWithVue, document) || + vscode.workspace.fs.isWritableFileSystem(document.uri.scheme) === false + ) + return + if (document.languageId === 'vue') return // todo + const importRegex = /^\s*import\((['"].*['"])\) from (['"].*['"])$/ + // const prevLine = + }) } From 64ba49dff01ee0579fbb3a87b2f25ced81bcfc00 Mon Sep 17 00:00:00 2001 From: Vitaly Turovsky Date: Sun, 19 Nov 2023 16:48:10 +0300 Subject: [PATCH 4/4] add import on enter action --- src/extension.ts | 2 ++ src/onEnterActions.ts | 33 +++++++++++++++++++++++++++------ 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index 51d6d76..fc557d9 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -17,6 +17,7 @@ import moreCompletions from './moreCompletions' import { mergeSettingsFromScopes } from './mergeSettings' import codeActionProvider from './codeActionProvider' import nonTsCommands from './nonTsCommands' +import onEnterActions from './onEnterActions' let isActivated = false // let erroredStatusBarItem: vscode.StatusBarItem | undefined @@ -96,6 +97,7 @@ export const activateTsPlugin = (tsApi: { configurePlugin; onCompletionAccepted figIntegration() vueVolarSupport() + onEnterActions() if (process.env.PLATFORM === 'node' && process.env.NODE_ENV === 'development') { require('./autoPluginReload').default() diff --git a/src/onEnterActions.ts b/src/onEnterActions.ts index 77561c5..f23f673 100644 --- a/src/onEnterActions.ts +++ b/src/onEnterActions.ts @@ -1,15 +1,36 @@ -import { defaultJsSupersetLangsWithVue } from '@zardoy/vscode-utils/build/langs' import * as vscode from 'vscode' +import { defaultJsSupersetLangsWithVue } from '@zardoy/vscode-utils/build/langs' + export default () => { vscode.workspace.onDidChangeTextDocument(({ contentChanges, document, reason }) => { if ( - !contentChanges.length || + contentChanges.length === 0 || !vscode.languages.match(defaultJsSupersetLangsWithVue, document) || vscode.workspace.fs.isWritableFileSystem(document.uri.scheme) === false - ) + ) { return - if (document.languageId === 'vue') return // todo - const importRegex = /^\s*import\((['"].*['"])\) from (['"].*['"])$/ - // const prevLine = + } + + if (!contentChanges.some(change => !isEol(change.text))) return + // if (document.languageId === 'vue') return // todo + const importRegex = /^\s*import(.*) from (['"].*['"])/gi + const prevLine = document.lineAt(contentChanges[0]!.range.start.line) + if (importRegex.test(prevLine.text)) { + const lines = document.getText().split('\n') + let lineToInsert = 0 + for (const [i, line] of lines.entries()) { + if (!line.trim() || line.trim().startsWith('import') || line.trim().startsWith('require') || /^(#|\/\/|\/\*)/.test(line)) continue + lineToInsert = i + break + } + + const editor = vscode.window.activeTextEditor! + void editor.edit(b => { + b.delete(prevLine.rangeIncludingLineBreak) + b.insert(new vscode.Position(lineToInsert, 0), `${prevLine.text.trim()}\n`) + }) + } }) } + +const isEol = (text: string) => (text.startsWith('/n') || text.startsWith('/r/n')) && text.trim() === ''