Skip to content

Commit cd1ea3d

Browse files
committed
reapply fix for method snippet with code actions for windows in really verbose way
1 parent 94f26c5 commit cd1ea3d

File tree

5 files changed

+39
-3
lines changed

5 files changed

+39
-3
lines changed

README.MD

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,12 @@ function Foo() {
315315
}
316316
```
317317

318+
`tsEssentialPlugins.methodSnippetsInsertText`:
319+
320+
Optionally resolve insertText of all completion at suggest trigger:
321+
322+
![method-snippets-insert-text](media/method-snippets-insert-text.png)
323+
318324
### Ambiguous Suggestions
319325

320326
Some variables like `Object` or `lodash` are common to access properties as well as call directly:

media/method-snippets-insert-text.png

23.4 KB
Loading

src/onCompletionAccepted.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,22 @@ export default (tsApi: { onCompletionAccepted }) => {
4343
const dataMarker = '<!--tep '
4444
if (!documentation?.startsWith(dataMarker)) return
4545
const parsed = JSON.parse(documentation.slice(dataMarker.length, documentation.indexOf('e-->')))
46-
const { methodSnippet: params, isAmbiguous } = parsed
47-
// nextChar check also duplicated in completionEntryDetails for perf, but we need to run this check again with correct position
46+
const { methodSnippet: params, isAmbiguous, wordStartOffset } = parsed
4847
const startPos = editor.selection.start
48+
const acceptedWordStartOffset = wordStartOffset !== undefined && editor.document.getWordRangeAtPosition(startPos, /[\w\d]+/i)?.start
49+
if (!oneOf(acceptedWordStartOffset, false, undefined) && wordStartOffset === editor.document.offsetAt(acceptedWordStartOffset)) {
50+
await new Promise<void>(resolve => {
51+
vscode.workspace.onDidChangeTextDocument(({ document, contentChanges }) => {
52+
if (document !== editor.document || contentChanges.length === 0) return
53+
resolve()
54+
})
55+
})
56+
await new Promise(resolve => {
57+
setTimeout(resolve, 0)
58+
})
59+
}
60+
61+
// nextChar check also duplicated in completionEntryDetails for perf, but we need to run this check again with correct position
4962
const nextChar = editor.document.getText(new vscode.Range(startPos, startPos.translate(0, 1)))
5063
if (!params || ['(', '.', '`'].includes(nextChar)) return
5164

typescript/src/completionEntryDetails.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import constructMethodSnippet from './constructMethodSnippet'
33
import { RequestResponseTypes } from './ipcTypes'
44
import namespaceAutoImports from './namespaceAutoImports'
55
import { GetConfig } from './types'
6+
import { wordStartAtPos } from './utils'
67

78
export const lastResolvedCompletion = {
89
value: undefined as undefined | RequestResponseTypes['getLastResolvedCompletion'],
@@ -60,7 +61,12 @@ export default function completionEntryDetails(
6061
}
6162
const methodSnippet = constructMethodSnippet(languageService, sourceFile, position, symbol, c, resolveData)
6263
if (methodSnippet) {
63-
const data = JSON.stringify({ methodSnippet, isAmbiguous: resolveData.isAmbiguous })
64+
const wordStartOffset = source ? wordStartAtPos(sourceFile.getFullText(), position) : undefined
65+
const data = JSON.stringify({
66+
methodSnippet,
67+
isAmbiguous: resolveData.isAmbiguous,
68+
wordStartOffset,
69+
})
6470
prior.documentation = [{ kind: 'text', text: `<!--tep ${data} e-->` }, ...(prior.documentation ?? [])]
6571
}
6672
}

typescript/src/utils.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,17 @@ const wordRangeAtPos = (text: string, position: number) => {
258258
return text.slice(startPos + 1, endPos)
259259
}
260260

261+
export const wordStartAtPos = (text: string, position: number) => {
262+
const isGood = (pos: number) => {
263+
return /[\w\d]/i.test(text.at(pos - 1) ?? '')
264+
}
265+
let startPos = position
266+
while (isGood(startPos)) {
267+
startPos--
268+
}
269+
return startPos
270+
}
271+
261272
type GetIs<T> = T extends (elem: any) => elem is infer T ? T : never
262273

263274
export const createDummySourceFile = (code: string) => {

0 commit comments

Comments
 (0)