Skip to content

Commit 3690446

Browse files
committed
fix don't add space to some keywords in types
1 parent 3bc7d2a commit 3690446

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

typescript/src/completionsAtPosition.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type tslib from 'typescript/lib/tsserverlibrary'
33
// import * as emmet from '@vscode/emmet-helper'
44
import isInBannedPosition from './completions/isInBannedPosition'
55
import { GetConfig } from './types'
6-
import { findChildContainingPosition } from './utils'
6+
import { findChildContainingExactPosition, findChildContainingPosition } from './utils'
77
import indexSignatureAccessCompletions from './completions/indexSignatureAccess'
88
import fixPropertiesSorting from './completions/fixPropertiesSorting'
99
import { isGoodPositionBuiltinMethodCompletion } from './completions/isGoodPositionMethodCompletion'
@@ -49,7 +49,7 @@ export const getCompletionsAtPosition = (
4949
/** node that is one character behind
5050
* useful as in most cases we work with node that is behind the cursor */
5151
const leftNode = findChildContainingPosition(ts, sourceFile, position - 1)
52-
const tokenAtPosition = tsFull.getTokenAtPosition(sourceFile as any, position) as ts.Node
52+
const exactNode = findChildContainingExactPosition(sourceFile, position)
5353
if (['.jsx', '.tsx'].some(ext => fileName.endsWith(ext))) {
5454
// #region JSX tag improvements
5555
if (node) {
@@ -160,7 +160,7 @@ export const getCompletionsAtPosition = (
160160
})
161161

162162
if (c('suggestions.keywordsInsertText') === 'space') {
163-
prior.entries = keywordsSpace(prior.entries, scriptSnapshot, position, tokenAtPosition)
163+
prior.entries = keywordsSpace(prior.entries, scriptSnapshot, position, exactNode)
164164
}
165165

166166
if (leftNode && c('switchExcludeCoveredCases')) prior.entries = switchCaseExcludeCovered(prior.entries, position, sourceFile, leftNode) ?? prior.entries

typescript/src/utils.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@ export function findChildContainingPosition(typescript: typeof ts, sourceFile: t
1111
return find(sourceFile)
1212
}
1313

14+
export function findChildContainingExactPosition(sourceFile: ts.SourceFile, position: number): ts.Node | undefined {
15+
function find(node: ts.Node): ts.Node | undefined {
16+
if (position >= node.getStart() && position <= node.getEnd()) {
17+
return ts.forEachChild(node, find) || node
18+
}
19+
20+
return
21+
}
22+
return find(sourceFile)
23+
}
24+
1425
export function findChildContainingPositionMaxDepth(sourceFile: ts.SourceFile, position: number, maxDepth?: number): ts.Node | undefined {
1526
let currentDepth = 0
1627
function find(node: ts.Node): ts.Node | undefined {
@@ -114,3 +125,21 @@ export const isWeb = () => {
114125
return true
115126
}
116127
}
128+
129+
export function addObjectMethodResultInterceptors<T extends Record<string, any>>(
130+
object: T,
131+
interceptors: Partial<{
132+
[K in keyof Required<T> as T[K] extends (...args: any[]) => any ? K : never]: (result: ReturnType<T[K]>, ...args: Parameters<T[K]>) => ReturnType<T[K]>
133+
}>,
134+
) {
135+
for (const key of Object.keys(interceptors)) {
136+
const x = object[key]!
137+
const callback = interceptors[key]!
138+
if (typeof x !== 'function') continue
139+
//@ts-ignore
140+
object[key] = (...args: any) => {
141+
const result = x.apply(object, args)
142+
return callback(result, ...args)
143+
}
144+
}
145+
}

0 commit comments

Comments
 (0)