Skip to content

Commit

Permalink
feat: Add auto-completion trigger for JSX attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
zardoy committed May 24, 2024
1 parent 8a11e71 commit 54b4acd
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/autoCompletionsTrigger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as vscode from 'vscode'
import { defaultLanguageSupersets } from '@zardoy/vscode-utils/build/langs'
import { getExtensionSetting } from 'vscode-framework'
import { sendCommand } from './sendCommand'

const jsxAttributesAutoTrigger = () => {
vscode.workspace.onDidChangeTextDocument(async ({ contentChanges, document, reason }) => {
const editor = vscode.window.activeTextEditor
if (document !== editor?.document || contentChanges.length === 0) return
if (contentChanges[0]!.text !== ' ') return
if (![...defaultLanguageSupersets.react, 'javascript'].includes(document.languageId)) return
if (!getExtensionSetting('completionsAutoTrigger.jsx')) return
const path = await sendCommand('getNodePath', { document, position: editor.selection.active })
if (!path) return
if (['JsxSelfClosingElement', 'JsxOpeningElement'].includes(path.at(-1)?.kindName ?? '')) {
await vscode.commands.executeCommand('editor.action.triggerSuggest')
}
})
}

export default () => {
jsxAttributesAutoTrigger()
}
4 changes: 4 additions & 0 deletions src/configurationType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,10 @@ export type Configuration = {
iconPost?: string
}
}
/**
* @default true
*/
'completionsAutoTrigger.jsx': boolean
/**
* @default false
*/
Expand Down
2 changes: 2 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { mergeSettingsFromScopes } from './mergeSettings'
import codeActionProvider from './codeActionProvider'
import nonTsCommands from './nonTsCommands'
import inlayHints from './inlayHints'
import autoCompletionsTrigger from './autoCompletionsTrigger'

let isActivated = false
// let erroredStatusBarItem: vscode.StatusBarItem | undefined
Expand Down Expand Up @@ -98,6 +99,7 @@ export const activateTsPlugin = (tsApi: { configurePlugin; onCompletionAccepted
figIntegration()
vueVolarSupport()
inlayHints()
autoCompletionsTrigger()

if (process.env.PLATFORM === 'node' && process.env.NODE_ENV === 'development') {
require('./autoPluginReload').default()
Expand Down

0 comments on commit 54b4acd

Please sign in to comment.