Skip to content

Commit a257ef7

Browse files
committed
feat: disable by default method snippet methods in jsx attributes to not be annoying when passing callbacks for attributes starting with on
1 parent a51ec55 commit a257ef7

File tree

5 files changed

+24
-11
lines changed

5 files changed

+24
-11
lines changed

src/configurationType.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,11 @@ export type Configuration = {
101101
* */
102102
'jsxEmmet.type': 'realEmmet' | 'fakeEmmet' | 'disabled'
103103
/**
104-
* Sorting matters
104+
* Note: Sorting matters
105105
*/
106106
'jsxPseudoEmmet.tags': { [tag: string]: true | string }
107107
/**
108-
* Exclude lowercase / incorrent e.g. suggestions
108+
* Exclude lowercase / incorrent suggestions
109109
* @default true
110110
*/
111111
'jsxImproveElementsSuggestions.enabled': boolean
@@ -163,6 +163,12 @@ export type Configuration = {
163163
* @default true
164164
*/
165165
enableMethodSnippets: boolean
166+
/**
167+
* Wether to disable our and builtin method snippets within jsx attributes
168+
* @default true
169+
*/
170+
// TODO add smart setting
171+
'disableMethodSnippets.jsxAttributes': boolean
166172
/**
167173
* Support `@ts-diagnostic-disable` top-level comment for disabling spefici semantic diagnostics
168174
* Example: `// @ts-diagnostic-disable
@@ -181,8 +187,8 @@ export type Configuration = {
181187
*/
182188
patchOutline: boolean
183189
/**
184-
* Improve JSX completions:
185-
* - enable fixes
190+
* Improve JSX attribute completions:
191+
* - enable builtin jsx attribute completion fix
186192
* - enable jsxCompletionsMap
187193
* @default true
188194
*/

typescript/src/completions/isGoodPositionMethodCompletion.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import type tslib from 'typescript/lib/tsserverlibrary'
2+
import { GetConfig } from '../types'
23
import { findChildContainingPosition, findChildContainingPositionMaxDepth } from '../utils'
34

4-
export const isGoodPositionBuiltinMethodCompletion = (ts: typeof tslib, sourceFile: tslib.SourceFile, position: number) => {
5+
export const isGoodPositionBuiltinMethodCompletion = (ts: typeof tslib, sourceFile: tslib.SourceFile, position: number, c: GetConfig) => {
56
const importClauseCandidate = findChildContainingPositionMaxDepth(ts, sourceFile, position, 3)
67
if (importClauseCandidate && ts.isImportClause(importClauseCandidate)) return false
78
const textBeforePos = sourceFile.getFullText().slice(position - 1, position)
@@ -14,6 +15,7 @@ export const isGoodPositionBuiltinMethodCompletion = (ts: typeof tslib, sourceFi
1415
if (ts.isJsxSelfClosingElement(currentNode) || ts.isJsxOpeningElement(currentNode)) return false
1516
if (ts.isShorthandPropertyAssignment(currentNode)) currentNode = currentNode.parent
1617
if (ts.isObjectBindingPattern(currentNode) || ts.isObjectLiteralExpression(currentNode)) return false
18+
if (c('disableMethodSnippets.jsxAttributes') && ts.isJsxExpression(currentNode)) return false
1719
}
1820
return true
1921
}
@@ -24,8 +26,9 @@ export const isGoodPositionMethodCompletion = (
2426
sourceFile: tslib.SourceFile,
2527
position: number,
2628
languageService: tslib.LanguageService,
29+
c: GetConfig,
2730
) => {
28-
if (!isGoodPositionBuiltinMethodCompletion(ts, sourceFile, position)) return false
31+
if (!isGoodPositionBuiltinMethodCompletion(ts, sourceFile, position, c)) return false
2932
// const { kind, displayParts } = languageService.getQuickInfoAtPosition(fileName, position) ?? {}
3033
// console.log('kind', kind, displayParts?.map(({ text }) => text).join(''))
3134
// switch (kind) {

typescript/src/completionsAtPosition.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ export const getCompletionsAtPosition = (
229229
}
230230

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

typescript/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export = ({ typescript }: { typescript: typeof ts }) => {
9494
)
9595
) {
9696
// - 1 to look for possibly previous completing item
97-
let goodPosition = isGoodPositionMethodCompletion(ts, fileName, sourceFile, position - 1, info.languageService)
97+
let goodPosition = isGoodPositionMethodCompletion(ts, fileName, sourceFile, position - 1, info.languageService, c)
9898
let rawPartsOverride: ts.SymbolDisplayPart[] | undefined
9999
if (goodPosition && prior.kind === ts.ScriptElementKind.alias) {
100100
goodPosition =

typescript/test/completions.spec.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,11 @@ test('Builtin method snippet banned positions', () => {
6666
const {something, met/*|*/} = test()
6767
;<Test/*|*/ />
6868
;<Test/*|*/></Test>
69+
;<Test test={/*|*/}></Test>
70+
;<Test test={a/*|*/}></Test>
6971
`)
7072
for (const [i, pos] of cursorPositions.entries()) {
71-
const result = isGoodPositionBuiltinMethodCompletion(ts, getSourceFile(), pos)
73+
const result = isGoodPositionBuiltinMethodCompletion(ts, getSourceFile(), pos, defaultConfigFunc)
7274
expect(result, i.toString()).toBeFalsy()
7375
}
7476
const insertTextEscaping = getCompletionsAtPosition(cursorPositions[1]!)!.entries[1]?.insertText!
@@ -86,9 +88,11 @@ test('Additional banned positions for our method snippets', () => {
8688
})
8789
;<Test/*|*/ />
8890
;<Test/*|*/></Test>
91+
;<Test test={/*|*/}></Test>
92+
;<Test test={a/*|*/}></Test>
8993
`)
9094
for (const [i, pos] of cursorPositions.entries()) {
91-
const result = isGoodPositionMethodCompletion(ts, entrypoint, getSourceFile(), pos - 1, languageService)
95+
const result = isGoodPositionMethodCompletion(ts, entrypoint, getSourceFile(), pos - 1, languageService, defaultConfigFunc)
9296
expect(result, i.toString()).toBeFalsy()
9397
}
9498
})
@@ -106,7 +110,7 @@ test('Not banned positions for our method snippets', () => {
106110
test2/*|*/
107111
`)
108112
for (const [i, pos] of cursorPositions.entries()) {
109-
const result = isGoodPositionMethodCompletion(ts, entrypoint, getSourceFile(), pos - 1, languageService)
113+
const result = isGoodPositionMethodCompletion(ts, entrypoint, getSourceFile(), pos - 1, languageService, defaultConfigFunc)
110114
expect(result, i.toString()).toBeTruthy()
111115
}
112116
})

0 commit comments

Comments
 (0)