Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

On enter actions #185

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -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()
36 changes: 36 additions & 0 deletions src/onEnterActions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import * as vscode from 'vscode'
import { defaultJsSupersetLangsWithVue } from '@zardoy/vscode-utils/build/langs'

export default () => {
vscode.workspace.onDidChangeTextDocument(({ contentChanges, document, reason }) => {
if (
contentChanges.length === 0 ||
!vscode.languages.match(defaultJsSupersetLangsWithVue, document) ||
vscode.workspace.fs.isWritableFileSystem(document.uri.scheme) === false
) {
return
}

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() === ''
1 change: 1 addition & 0 deletions typescript/src/ipcTypes.ts
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@ export const triggerCharacterCommands = [
'getExtendedCodeActionEdits',
'getLastResolvedCompletion',
'getArgumentReferencesFromCurrentParameter',
'onEnterActions',
'performanceInfo',
'getMigrateToImportsEdits',
] as const
3 changes: 3 additions & 0 deletions typescript/src/specialCommands/handle.ts
Original file line number Diff line number Diff line change
@@ -314,6 +314,9 @@ export default (
return combinedCodeFix.changes[0]?.textChanges
}

if (specialCommand === 'onEnterActions') {
}

return null
}