Skip to content

Commit 41cfff0

Browse files
committed
use real cancellation token
1 parent 02eaa55 commit 41cfff0

File tree

3 files changed

+29
-12
lines changed

3 files changed

+29
-12
lines changed

typescript/src/getPatchedNavTree.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isTs5, nodeModules } from './utils'
1+
import { getCancellationToken, isTs5, nodeModules } from './utils'
22
import { createLanguageService } from './dummyLanguageService'
33
import { getCannotFindCodes } from './utils/cannotFindCodes'
44

@@ -168,9 +168,5 @@ export const getNavTreeItems = (info: ts.server.PluginCreateInfo, fileName: stri
168168
const sourceFile = program?.getSourceFile(fileName)
169169
if (!sourceFile) throw new Error('no sourceFile')
170170

171-
const cancellationToken = info.languageServiceHost.getCompilerHost?.()?.getCancellationToken?.() ?? {
172-
isCancellationRequested: () => false,
173-
throwIfCancellationRequested: () => {},
174-
}
175-
return navModule.getNavigationTree(sourceFile, cancellationToken)
171+
return navModule.getNavigationTree(sourceFile, getCancellationToken(info.languageServiceHost))
176172
}

typescript/src/utils.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,30 @@ export function addObjectMethodResultInterceptors<T extends Record<string, any>>
167167
}
168168
}
169169

170+
// have absolutely no idea why such useful utility is not publicly available
171+
export const getChangesTracker = formatOptions => {
172+
return new tsFull.textChanges.ChangeTracker(/* will be normalized by vscode anyway */ '\n', tsFull.formatting.getFormatContext(formatOptions, {}))
173+
}
174+
175+
export const getCancellationToken = (languageServiceHost: ts.LanguageServiceHost) => {
176+
let cancellationToken = languageServiceHost.getCancellationToken?.() as ts.CancellationToken | undefined
177+
// if (!cancellationToken) {
178+
// debugger
179+
// }
180+
cancellationToken ??= {
181+
isCancellationRequested: () => false,
182+
throwIfCancellationRequested: () => {},
183+
}
184+
if (!cancellationToken.throwIfCancellationRequested) {
185+
cancellationToken.throwIfCancellationRequested = () => {
186+
if (cancellationToken!.isCancellationRequested()) {
187+
throw new ts.OperationCanceledException()
188+
}
189+
}
190+
}
191+
return cancellationToken
192+
}
193+
170194
const wordRangeAtPos = (text: string, position: number) => {
171195
const isGood = (pos: number) => {
172196
return /[-\w\d]/i.test(text.at(pos) ?? '')

typescript/src/workspaceSymbolSearch.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { GetConfig } from './types'
2+
import { getCancellationToken } from './utils'
23

34
export default (proxy: ts.LanguageService, languageService: ts.LanguageService, c: GetConfig, languageServiceHost: ts.LanguageServiceHost) => {
45
proxy.getNavigateToItems = (searchValue, maxResultCount, fileName, excludeDtsFiles) => {
@@ -8,10 +9,7 @@ export default (proxy: ts.LanguageService, languageService: ts.LanguageService,
89
}
910

1011
const program = languageService.getProgram()!
11-
const cancellationToken = languageServiceHost.getCompilerHost?.()?.getCancellationToken?.() ?? {
12-
isCancellationRequested: () => false,
13-
throwIfCancellationRequested: () => {},
14-
}
12+
1513
let sourceFiles = fileName ? [program.getSourceFile(fileName)!] : program.getSourceFiles()
1614
if (!fileName) {
1715
const excludes = tsFull.getRegularExpressionForWildcard(workspaceSymbolSearchExcludePatterns, '', 'exclude')?.slice(1)
@@ -23,8 +21,7 @@ export default (proxy: ts.LanguageService, languageService: ts.LanguageService,
2321
return tsFull.NavigateTo.getNavigateToItems(
2422
sourceFiles as any,
2523
program.getTypeChecker() as any,
26-
// TODO! use real cancellationToken
27-
cancellationToken,
24+
getCancellationToken(languageServiceHost),
2825
searchValue,
2926
maxResultCount,
3027
excludeDtsFiles ?? false,

0 commit comments

Comments
 (0)