Skip to content

Commit 252feb1

Browse files
committed
fix: don't expand builtin and our method snippets at jsx tag positions!
fix: allow to expand builtin method snippets in property assignment
1 parent 7ef2839 commit 252feb1

File tree

4 files changed

+23
-12
lines changed

4 files changed

+23
-12
lines changed

README.MD

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ Add JSX elements to outline. It also makes sticky scroll works with your tags!
1010

1111
Super recommended for react. Fragments are not rendered.
1212

13+
## **Completions Built Different**
14+
15+
90% work done in this extension highly improves completions experience!
16+
1317
### Method Snippets
1418

1519
(*enabled by default*)

typescript/src/completionsAtPosition.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ export const getCompletionsAtPosition = (
227227
}
228228

229229
// prevent vscode-builtin wrong insertText with methods snippets enabled
230-
if (!isGoodPositionBuiltinMethodCompletion(ts, sourceFile, position)) {
230+
if (!isGoodPositionBuiltinMethodCompletion(ts, sourceFile, position - 1)) {
231231
prior.entries = prior.entries.map(item => {
232232
if (item.isSnippet) return item
233233
return { ...item, insertText: (item.insertText ?? item.name).replace(/\$/g, '\\$'), isSnippet: true }

typescript/src/isGoodPositionMethodCompletion.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export const isGoodPositionBuiltinMethodCompletion = (ts: typeof tslib, sourceFi
1111
// type A = typeof obj["|"]
1212
if (ts.isStringLiteralLike(currentNode)) return false
1313
if (ts.isIdentifier(currentNode)) currentNode = currentNode.parent
14+
if (ts.isJsxSelfClosingElement(currentNode) || ts.isJsxOpeningElement(currentNode)) return false
1415
if (ts.isShorthandPropertyAssignment(currentNode)) currentNode = currentNode.parent
1516
if (ts.isObjectBindingPattern(currentNode) || ts.isObjectLiteralExpression(currentNode)) return false
1617
}

typescript/test/completions.spec.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ const newFileContents = (contents: string, fileName = entrypoint) => {
2020
const cursorPositions: number[] = []
2121
const replacement = '/*|*/'
2222
let cursorIndex
23-
while ((cursorIndex = contents.indexOf(replacement))) {
24-
if (cursorIndex === -1) break
23+
while ((cursorIndex = contents.indexOf(replacement)) !== -1) {
2524
contents = contents.slice(0, cursorIndex) + contents.slice(cursorIndex + replacement.length)
2625
cursorPositions.push(cursorIndex)
2726
}
@@ -58,13 +57,15 @@ test('Banned positions', () => {
5857
})
5958

6059
test('Builtin method snippet banned positions', () => {
61-
const cursorPositions = newFileContents(/* ts */ `
62-
import {/*|*/} from 'test'
63-
const obj = { m$1e$2thod() {}, arrow: () => {} }
64-
type A = typeof obj["/*|*/"];
65-
const test = () => ({ method() {} })
66-
const {/*|*/} = test()
67-
const {something, met/*|*/} = test()
60+
const cursorPositions = newFileContents(/* tsx */ `
61+
import {/*|*/} from 'test'
62+
const obj = { m$1e$2thod() {}, arrow: () => {} }
63+
type A = typeof obj["/*|*/"];
64+
const test = () => ({ method() {} })
65+
const {/*|*/} = test()
66+
const {something, met/*|*/} = test()
67+
;<Test/*|*/ />
68+
;<Test/*|*/></Test>
6869
`)
6970
for (const [i, pos] of cursorPositions.entries()) {
7071
const result = isGoodPositionBuiltinMethodCompletion(ts, getSourceFile(), pos)
@@ -75,17 +76,19 @@ test('Builtin method snippet banned positions', () => {
7576
})
7677

7778
test('Additional banned positions for our method snippets', () => {
78-
const cursorPositions = newFileContents(/* ts */ `
79+
const cursorPositions = newFileContents(/* tsx */ `
7980
const test = () => ({ method() {} })
8081
test({
8182
method/*|*/
8283
})
8384
test({
8485
/*|*/
8586
})
87+
;<Test/*|*/ />
88+
;<Test/*|*/></Test>
8689
`)
8790
for (const [i, pos] of cursorPositions.entries()) {
88-
const result = isGoodPositionMethodCompletion(ts, entrypoint, getSourceFile(), pos, languageService)
91+
const result = isGoodPositionMethodCompletion(ts, entrypoint, getSourceFile(), pos - 1, languageService)
8992
expect(result, i.toString()).toBeFalsy()
9093
}
9194
})
@@ -97,6 +100,9 @@ test('Not banned positions for our method snippets', () => {
97100
test({
98101
method: /*|*/
99102
})
103+
test({
104+
method: setTimeout/*|*/
105+
})
100106
test2/*|*/
101107
`)
102108
for (const [i, pos] of cursorPositions.entries()) {

0 commit comments

Comments
 (0)