Skip to content

Commit 66d0a88

Browse files
committed
feat: Add suggestions.keywordsInsertText settings
starting from now, all keywords will now insert space after its name. You can disable it with setting above chore: formatting and gitignore
1 parent 7b38ad1 commit 66d0a88

File tree

7 files changed

+108
-44
lines changed

7 files changed

+108
-44
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ out
1010
.nuxt
1111
.cache
1212
src/generated.ts
13+
src/configurationType.js
1314

1415
coverage
1516
*.lcov

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
"dependencies": {
5151
"@types/lodash": "^4.14.182",
5252
"@vscode/emmet-helper": "^2.8.4",
53-
"@zardoy/vscode-utils": "^0.0.9",
53+
"@zardoy/vscode-utils": "^0.0.14",
5454
"chokidar": "^3.5.3",
5555
"eslint": "^8.7.0",
5656
"eslint-config-zardoy": "^0.2.8",
@@ -66,6 +66,6 @@
6666
"tabWidth": 4,
6767
"trailingComma": "all",
6868
"arrowParens": "avoid",
69-
"printWidth": 150
69+
"printWidth": 160
7070
}
7171
}

pnpm-lock.yaml

Lines changed: 56 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/configurationType.js

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/configurationType.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ export type Configuration = {
4747
// TODO achieve perfomace by patching the host
4848
/** @default [] */
4949
'suggestions.banAutoImportPackages': string[]
50+
/**
51+
* What insert text to use for keywords (e.g. `return`)
52+
* @default space
53+
*/
54+
'suggestions.keywordsInsertText': 'none' | 'space'
5055
// TODO! corrent watching!
5156
/**
5257
*

src/configurationTypeCache.jsonc

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// GENERATED. DON'T EDIT MANUALLY
2-
// md5hash: 0b8b4697ce68ebfe40346fd51700a786
2+
// md5hash: d3897fbc226e0979fe8f8b4348bf9b6d
33
{
44
"type": "object",
55
"properties": {
@@ -20,6 +20,15 @@
2020
"type": "string"
2121
}
2222
},
23+
"suggestions.keywordsInsertText": {
24+
"description": "What insert text to use for keywords (e.g. `return`)",
25+
"default": "space",
26+
"enum": [
27+
"none",
28+
"space"
29+
],
30+
"type": "string"
31+
},
2332
"highlightNonFunctionMethods.enable": {
2433
"description": "Highlight and lift non-function methods. Also applies for static class methods. Uses `bind`, `call`, `caller` detection.",
2534
"default": true,
@@ -237,6 +246,7 @@
237246
"removeCodeFixes.enable",
238247
"removeUselessFunctionProps.enable",
239248
"replaceSuggestions",
240-
"suggestions.banAutoImportPackages"
249+
"suggestions.banAutoImportPackages",
250+
"suggestions.keywordsInsertText"
241251
]
242252
}

typescript/src/index.ts

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as emmet from '@vscode/emmet-helper'
66
//@ts-ignore
77
import type { Configuration } from '../../src/configurationType'
88

9-
export = function({ typescript }: { typescript: typeof import('typescript/lib/tsserverlibrary') }) {
9+
export = function ({ typescript }: { typescript: typeof import('typescript/lib/tsserverlibrary') }) {
1010
const ts = typescript
1111
let _configuration: Configuration
1212
const c = <T extends keyof Configuration>(key: T): Configuration[T] => get(_configuration, key)
@@ -163,24 +163,24 @@ export = function({ typescript }: { typescript: typeof import('typescript/lib/ts
163163
const entryNames = new Set(prior.entries.map(({ name }) => name))
164164
if (c('removeUselessFunctionProps.enable')) prior.entries = prior.entries.filter(e => !['Symbol', 'caller', 'prototype'].includes(e.name))
165165
if (['bind', 'call', 'caller'].every(name => entryNames.has(name)) && c('highlightNonFunctionMethods.enable')) {
166-
const standardProps = new Set(['Symbol', 'apply', 'arguments', 'bind', 'call', 'caller', 'length', 'name', 'prototype', 'toString'])
167-
// TODO lift up!
168-
prior.entries = prior.entries.map(entry => {
169-
if (!standardProps.has(entry.name) && entry.kind !== ts.ScriptElementKind.warning) {
170-
const newName = `☆${entry.name}`
171-
prevCompletionsMap[newName] = {
172-
originalName: entry.name,
173-
}
174-
return {
175-
...entry,
176-
insertText: entry.insertText ?? entry.name,
177-
name: newName,
178-
}
166+
const standardProps = new Set(['Symbol', 'apply', 'arguments', 'bind', 'call', 'caller', 'length', 'name', 'prototype', 'toString'])
167+
// TODO lift up!
168+
prior.entries = prior.entries.map(entry => {
169+
if (!standardProps.has(entry.name) && entry.kind !== ts.ScriptElementKind.warning) {
170+
const newName = `☆${entry.name}`
171+
prevCompletionsMap[newName] = {
172+
originalName: entry.name,
173+
}
174+
return {
175+
...entry,
176+
insertText: entry.insertText ?? entry.name,
177+
name: newName,
179178
}
179+
}
180180

181-
return entry
182-
})
183-
}
181+
return entry
182+
})
183+
}
184184

185185
if (c('patchToString.enable')) {
186186
// const indexToPatch = arrayMoveItemToFrom(
@@ -205,6 +205,16 @@ export = function({ typescript }: { typescript: typeof import('typescript/lib/ts
205205
// TODO change to startsWith?
206206
return !banAutoImportPackages.includes(text)
207207
})
208+
209+
if (c('suggestions.keywordsInsertText') === 'space') {
210+
const charAhead = scriptSnapshot.getText(position, position + 1)
211+
prior.entries = prior.entries.map(entry => {
212+
if (entry.kind !== ts.ScriptElementKind.keyword) return entry
213+
entry.insertText = charAhead === ' ' ? entry.name : `${entry.name} `
214+
return entry
215+
})
216+
}
217+
208218
for (const rule of c('replaceSuggestions')) {
209219
let foundIndex: number
210220
const suggestion = prior.entries.find(({ name, kind }, index) => {
@@ -215,19 +225,15 @@ export = function({ typescript }: { typescript: typeof import('typescript/lib/ts
215225
})
216226
if (!suggestion) continue
217227

218-
if (rule.delete)
219-
prior.entries.splice(foundIndex!, 1)
220-
228+
if (rule.delete) prior.entries.splice(foundIndex!, 1)
221229

222-
if (rule.duplicateOriginal)
223-
prior.entries.splice(rule.duplicateOriginal === 'above' ? foundIndex! : foundIndex! + 1, 0, { ...suggestion })
230+
if (rule.duplicateOriginal) prior.entries.splice(rule.duplicateOriginal === 'above' ? foundIndex! : foundIndex! + 1, 0, { ...suggestion })
224231

225232
Object.assign(suggestion, rule.patch ?? {})
226233
if (rule.patch?.insertText) suggestion.isSnippet = true
227234
}
228235

229-
if (c('correctSorting.enable'))
230-
prior.entries = prior.entries.map((entry, index) => ({ ...entry, sortText: `${entry.sortText ?? ''}${index}` }))
236+
if (c('correctSorting.enable')) prior.entries = prior.entries.map((entry, index) => ({ ...entry, sortText: `${entry.sortText ?? ''}${index}` }))
231237

232238
// console.log('signatureHelp', JSON.stringify(info.languageService.getSignatureHelpItems(fileName, position, {})))
233239
// console.timeEnd('slow-down')
@@ -283,8 +289,7 @@ export = function({ typescript }: { typescript: typeof import('typescript/lib/ts
283289

284290
// @ts-expect-error some experiments
285291
proxy.ignored = (fileName: string, positionOrRange: number, preferences: any) => {
286-
if (typeof positionOrRange !== 'number')
287-
positionOrRange = positionOrRange
292+
if (typeof positionOrRange !== 'number') positionOrRange = positionOrRange
288293

289294
// ts.createSourceFile(fileName, sourceText, languageVersion)
290295
const { textSpan } = proxy.getSmartSelectionRange(fileName, positionOrRange)
@@ -351,8 +356,7 @@ function findChildContainingPosition(
351356
position: number,
352357
): tslib.Node | undefined {
353358
function find(node: ts.Node): ts.Node | undefined {
354-
if (position >= node.getStart() && position < node.getEnd())
355-
return typescript.forEachChild(node, find) || node
359+
if (position >= node.getStart() && position < node.getEnd()) return typescript.forEachChild(node, find) || node
356360

357361
return
358362
}

0 commit comments

Comments
 (0)