Skip to content

Commit

Permalink
feat: big feature! Args snippets for simple arrow callbacks (enabled …
Browse files Browse the repository at this point in the history
…by default).

Feature is experimental and doesn't support aliases for now
  • Loading branch information
zardoy committed Sep 11, 2022
1 parent 0765633 commit 8f3280f
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/configurationType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,9 @@ export type Configuration = {
* @default false
*/
changeDtsFileDefinitionToJs: boolean
/**
* Experimental. Also includes optional args
* @default true
*/
enableMethodSnippets: boolean
}
32 changes: 32 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import * as vscode from 'vscode'
import { getActiveRegularEditor } from '@zardoy/vscode-utils'
import {} from 'vscode-framework'

export const activate = async () => {
const tsExtension = vscode.extensions.getExtension('vscode.typescript-language-features')
Expand All @@ -21,4 +23,34 @@ export const activate = async () => {
if (affectsConfiguration(process.env.IDS_PREFIX!)) syncConfig()
})
syncConfig()

api.onCompletionAccepted((item: vscode.CompletionItem & { document: vscode.TextDocument }) => {
const enableMethodSnippets = vscode.workspace.getConfiguration(process.env.IDS_PREFIX, item.document).get('enableMethodSnippets')
const { documentation = '' } = item
const documentationString = documentation instanceof vscode.MarkdownString ? documentation.value : documentation
const insertFuncArgs = /<!-- insert-func: (.*)-->/.exec(documentationString)?.[1]
if (enableMethodSnippets && insertFuncArgs !== undefined) {
const editor = getActiveRegularEditor()!
const startPos = editor.selection.start
const nextSymbol = editor.document.getText(new vscode.Range(startPos, startPos.translate(0, 1)))
if (nextSymbol !== '(') {
const snippet = new vscode.SnippetString('')
snippet.appendText('(')
const args = insertFuncArgs.split(',')
for (const [i, arg] of args.entries()) {
if (!arg) continue
snippet.appendPlaceholder(arg)
if (i !== args.length - 1) snippet.appendText(', ')
}

snippet.appendText(')')
void editor.insertSnippet(snippet, undefined, {
undoStopAfter: false,
undoStopBefore: false,
})
if (vscode.workspace.getConfiguration('editor.parameterHints').get('enabled'))
void vscode.commands.executeCommand('editor.action.triggerParameterHints')
}
}
})
}
32 changes: 32 additions & 0 deletions typescript/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import type { Configuration } from '../../src/configurationType'
import _ from 'lodash'
import { GetConfig } from './types'
import { getCompletionsAtPosition, PrevCompletionMap } from './completionsAtPosition'
import { CompletionEntry } from 'typescript/lib/tsserverlibrary'
import { getParameterListParts } from './completionGetMethodParameters'
import { oneOf } from '@zardoy/utils'

const thisPluginMarker = Symbol('__essentialPluginsMarker__')

Expand Down Expand Up @@ -83,6 +86,35 @@ export = function ({ typescript }: { typescript: typeof import('typescript/lib/t
data,
)
if (!prior) return
if (c('enableMethodSnippets') && oneOf(prior.kind as string, ts.ScriptElementKind.constElement, 'property')) {
const punctuationIndex = prior.displayParts.findIndex(({ kind }) => kind === 'punctuation')
if (punctuationIndex !== 1) {
const isParsableMethod = prior.displayParts
// next is space
.slice(punctuationIndex + 2)
.map(({ text }) => text)
.join('')
.match(/\((.*)\) => /)
if (isParsableMethod) {
let firstArgMeet = false
const args = prior.displayParts
.filter(({ kind }, index, array) => {
if (kind !== 'parameterName') return false
if (array[index - 1]!.text === '(') {
if (!firstArgMeet) {
// bad parsing, as doesn't take second and more args
firstArgMeet = true
return true
}
return false
}
return true
})
.map(({ text }) => text)
prior.documentation = [...(prior.documentation ?? []), { kind: 'text', text: `<!-- insert-func: ${args.join(',')}-->` }]
}
}
}
// if (prior.kind === typescript.ScriptElementKind.constElement && prior.displayParts.map(item => item.text).join('').match(/: \(.+\) => .+/)) prior.codeActions?.push({
// description: '',
// changes: []
Expand Down

0 comments on commit 8f3280f

Please sign in to comment.