Skip to content

Commit

Permalink
feat: Add suggestions.keywordsInsertText settings
Browse files Browse the repository at this point in the history
starting from now, all keywords will now insert space after its name. You can disable it with setting above
chore: formatting and gitignore
  • Loading branch information
zardoy committed May 24, 2022
1 parent 7b38ad1 commit 66d0a88
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 44 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ out
.nuxt
.cache
src/generated.ts
src/configurationType.js

coverage
*.lcov
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"dependencies": {
"@types/lodash": "^4.14.182",
"@vscode/emmet-helper": "^2.8.4",
"@zardoy/vscode-utils": "^0.0.9",
"@zardoy/vscode-utils": "^0.0.14",
"chokidar": "^3.5.3",
"eslint": "^8.7.0",
"eslint-config-zardoy": "^0.2.8",
Expand All @@ -66,6 +66,6 @@
"tabWidth": 4,
"trailingComma": "all",
"arrowParens": "avoid",
"printWidth": 150
"printWidth": 160
}
}
66 changes: 56 additions & 10 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions src/configurationType.js

This file was deleted.

5 changes: 5 additions & 0 deletions src/configurationType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ export type Configuration = {
// TODO achieve perfomace by patching the host
/** @default [] */
'suggestions.banAutoImportPackages': string[]
/**
* What insert text to use for keywords (e.g. `return`)
* @default space
*/
'suggestions.keywordsInsertText': 'none' | 'space'
// TODO! corrent watching!
/**
*
Expand Down
14 changes: 12 additions & 2 deletions src/configurationTypeCache.jsonc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// GENERATED. DON'T EDIT MANUALLY
// md5hash: 0b8b4697ce68ebfe40346fd51700a786
// md5hash: d3897fbc226e0979fe8f8b4348bf9b6d
{
"type": "object",
"properties": {
Expand All @@ -20,6 +20,15 @@
"type": "string"
}
},
"suggestions.keywordsInsertText": {
"description": "What insert text to use for keywords (e.g. `return`)",
"default": "space",
"enum": [
"none",
"space"
],
"type": "string"
},
"highlightNonFunctionMethods.enable": {
"description": "Highlight and lift non-function methods. Also applies for static class methods. Uses `bind`, `call`, `caller` detection.",
"default": true,
Expand Down Expand Up @@ -237,6 +246,7 @@
"removeCodeFixes.enable",
"removeUselessFunctionProps.enable",
"replaceSuggestions",
"suggestions.banAutoImportPackages"
"suggestions.banAutoImportPackages",
"suggestions.keywordsInsertText"
]
}
60 changes: 32 additions & 28 deletions typescript/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as emmet from '@vscode/emmet-helper'
//@ts-ignore
import type { Configuration } from '../../src/configurationType'

export = function({ typescript }: { typescript: typeof import('typescript/lib/tsserverlibrary') }) {
export = function ({ typescript }: { typescript: typeof import('typescript/lib/tsserverlibrary') }) {
const ts = typescript
let _configuration: Configuration
const c = <T extends keyof Configuration>(key: T): Configuration[T] => get(_configuration, key)
Expand Down Expand Up @@ -163,24 +163,24 @@ export = function({ typescript }: { typescript: typeof import('typescript/lib/ts
const entryNames = new Set(prior.entries.map(({ name }) => name))
if (c('removeUselessFunctionProps.enable')) prior.entries = prior.entries.filter(e => !['Symbol', 'caller', 'prototype'].includes(e.name))
if (['bind', 'call', 'caller'].every(name => entryNames.has(name)) && c('highlightNonFunctionMethods.enable')) {
const standardProps = new Set(['Symbol', 'apply', 'arguments', 'bind', 'call', 'caller', 'length', 'name', 'prototype', 'toString'])
// TODO lift up!
prior.entries = prior.entries.map(entry => {
if (!standardProps.has(entry.name) && entry.kind !== ts.ScriptElementKind.warning) {
const newName = `☆${entry.name}`
prevCompletionsMap[newName] = {
originalName: entry.name,
}
return {
...entry,
insertText: entry.insertText ?? entry.name,
name: newName,
}
const standardProps = new Set(['Symbol', 'apply', 'arguments', 'bind', 'call', 'caller', 'length', 'name', 'prototype', 'toString'])
// TODO lift up!
prior.entries = prior.entries.map(entry => {
if (!standardProps.has(entry.name) && entry.kind !== ts.ScriptElementKind.warning) {
const newName = `☆${entry.name}`
prevCompletionsMap[newName] = {
originalName: entry.name,
}
return {
...entry,
insertText: entry.insertText ?? entry.name,
name: newName,
}
}

return entry
})
}
return entry
})
}

if (c('patchToString.enable')) {
// const indexToPatch = arrayMoveItemToFrom(
Expand All @@ -205,6 +205,16 @@ export = function({ typescript }: { typescript: typeof import('typescript/lib/ts
// TODO change to startsWith?
return !banAutoImportPackages.includes(text)
})

if (c('suggestions.keywordsInsertText') === 'space') {
const charAhead = scriptSnapshot.getText(position, position + 1)
prior.entries = prior.entries.map(entry => {
if (entry.kind !== ts.ScriptElementKind.keyword) return entry
entry.insertText = charAhead === ' ' ? entry.name : `${entry.name} `
return entry
})
}

for (const rule of c('replaceSuggestions')) {
let foundIndex: number
const suggestion = prior.entries.find(({ name, kind }, index) => {
Expand All @@ -215,19 +225,15 @@ export = function({ typescript }: { typescript: typeof import('typescript/lib/ts
})
if (!suggestion) continue

if (rule.delete)
prior.entries.splice(foundIndex!, 1)

if (rule.delete) prior.entries.splice(foundIndex!, 1)

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

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

if (c('correctSorting.enable'))
prior.entries = prior.entries.map((entry, index) => ({ ...entry, sortText: `${entry.sortText ?? ''}${index}` }))
if (c('correctSorting.enable')) prior.entries = prior.entries.map((entry, index) => ({ ...entry, sortText: `${entry.sortText ?? ''}${index}` }))

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

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

// ts.createSourceFile(fileName, sourceText, languageVersion)
const { textSpan } = proxy.getSmartSelectionRange(fileName, positionOrRange)
Expand Down Expand Up @@ -351,8 +356,7 @@ function findChildContainingPosition(
position: number,
): tslib.Node | undefined {
function find(node: ts.Node): ts.Node | undefined {
if (position >= node.getStart() && position < node.getEnd())
return typescript.forEachChild(node, find) || node
if (position >= node.getStart() && position < node.getEnd()) return typescript.forEachChild(node, find) || node

return
}
Expand Down

0 comments on commit 66d0a88

Please sign in to comment.