Skip to content

Commit f76e81b

Browse files
committed
feat: boost name suggestion after its declaration
1 parent 8033fa2 commit f76e81b

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

src/extension.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { defaultJsSupersetLangs } from '@zardoy/vscode-utils/build/langs'
44
import { Settings, extensionCtx, getExtensionSetting, getExtensionSettingId, registerExtensionCommand } from 'vscode-framework'
55
import { pickObj } from '@zardoy/utils'
66
import { watchExtensionSettings } from '@zardoy/vscode-utils/build/settings'
7+
import { ConditionalPick } from 'type-fest'
78
import webImports from './webImports'
89
import { sendCommand } from './sendCommand'
910
import { registerEmmet } from './emmet'
@@ -16,7 +17,6 @@ import vueVolarSupport from './vueVolarSupport'
1617
import moreCompletions from './moreCompletions'
1718
import { mergeSettingsFromScopes } from './mergeSettings'
1819
import codeActionProvider from './codeActionProvider'
19-
import { ConditionalPick } from 'type-fest'
2020

2121
let isActivated = false
2222
// let erroredStatusBarItem: vscode.StatusBarItem | undefined
@@ -144,12 +144,13 @@ export const activate = async () => {
144144
const registerDisableOptionalFeaturesCommand = () => {
145145
registerExtensionCommand('disableAllOptionalFeatures', async () => {
146146
const config = vscode.workspace.getConfiguration(process.env.IDS_PREFIX, null)
147-
const toDisable: [keyof Settings, any][] = []
147+
const toDisable: Array<[keyof Settings, any]> = []
148148
for (const optionalExperience of optionalExperiences) {
149149
const desiredKey = Array.isArray(optionalExperience) ? optionalExperience[0] : optionalExperience
150150
const desiredValue = Array.isArray(optionalExperience) ? optionalExperience[1] : false
151151
if (config.get(desiredKey) !== desiredValue) toDisable.push([desiredKey, desiredValue])
152152
}
153+
153154
const action = await vscode.window.showInformationMessage(
154155
`${toDisable.length} features are going to be disabled`,
155156
{ detail: '', modal: true },
@@ -160,20 +161,22 @@ const registerDisableOptionalFeaturesCommand = () => {
160161
switch (action) {
161162
case 'Write to settings NOW': {
162163
for (const [key, value] of toDisable) {
163-
config.update(key, value, vscode.ConfigurationTarget.Global)
164+
void config.update(key, value, vscode.ConfigurationTarget.Global)
164165
}
166+
165167
break
166168
}
169+
167170
case 'Copy settings': {
168-
vscode.env.clipboard.writeText(JSON.stringify(Object.fromEntries(toDisable), undefined, 4))
171+
await vscode.env.clipboard.writeText(JSON.stringify(Object.fromEntries(toDisable), undefined, 4))
169172
break
170173
}
171174
}
172175
})
173176
}
174177

175178
/** Experiences that are enabled out of the box */
176-
const optionalExperiences: (keyof ConditionalPick<Settings, boolean> | [keyof Settings, any])[] = [
179+
const optionalExperiences: Array<keyof ConditionalPick<Settings, boolean> | [keyof Settings, any]> = [
177180
'enableMethodSnippets',
178181
'removeUselessFunctionProps.enable',
179182
'patchToString.enable',

typescript/src/completions/boostNameSuggestions.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,37 @@ export default (
1717
const fileText = sourceFile.getFullText()
1818
const fileTextBeforePos = fileText.slice(0, position)
1919
const beforeConstNodeOffset = fileTextBeforePos.match(/(?:const|let) ([\w\d]*)$/i)?.[1]
20+
const nodeWithStatements = node => {
21+
return node && 'statements' in node && Array.isArray(node.statements) ? node : undefined
22+
}
23+
const statementsNode = nodeWithStatements(node) || nodeWithStatements(node.parent)
24+
// Workaround for current locality bonus & TS 5.1
25+
if (statementsNode) {
26+
const statements = statementsNode.statements as any[]
27+
const prevNode =
28+
statementsNode === node
29+
? [...statements].reverse().find((statement: ts.Node) => statement.pos + statement.getLeadingTriviaWidth() < position)
30+
: statements[statements.indexOf(node) - 1]
31+
if (prevNode && ts.isVariableStatement(prevNode) && prevNode.declarationList.declarations.length === 1) {
32+
const { name } = prevNode.declarationList.declarations[0]!
33+
if (ts.isIdentifier(name)) {
34+
const kind: ts.ScriptElementKind =
35+
prevNode.declarationList.flags & ts.NodeFlags.Const ? ts.ScriptElementKind.constElement : ts.ScriptElementKind.letElement
36+
entries = boostOrAddSuggestions(entries, [
37+
{
38+
name: name.text,
39+
kind,
40+
},
41+
])
42+
}
43+
}
44+
}
2045
/** false - pick all identifiers after cursor
2146
* node - pick identifiers that within node */
2247
let filterBlock: undefined | false | ts.Node
2348
if (beforeConstNodeOffset !== undefined) {
2449
const node = findChildContainingPosition(ts, sourceFile, position - beforeConstNodeOffset.length - 2)
25-
if (!node || !ts.isVariableDeclarationList(node)) return
50+
if (!node || !ts.isVariableDeclarationList(node)) return entries
2651
filterBlock = false
2752
} else if (ts.isIdentifier(node) && node.parent?.parent) {
2853
// node > parent1 > parent2
@@ -44,7 +69,7 @@ export default (
4469
}
4570
}
4671

47-
if (filterBlock === undefined) return
72+
if (filterBlock === undefined) return entries
4873
const semanticDiagnostics = languageService.getSemanticDiagnostics(sourceFile.fileName)
4974

5075
const notFoundIdentifiers = semanticDiagnostics

0 commit comments

Comments
 (0)