Skip to content

Commit d0fc1b3

Browse files
fix: performance issue from @Copilot in #213 (#214)
1 parent 1fdfcd3 commit d0fc1b3

1 file changed

Lines changed: 15 additions & 3 deletions

File tree

packages/language-server/src/patches/patch-semantic.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,19 @@ export function patchSemantic(typescriptServices: LanguageServicePlugin[], confi
3737
return foundNode
3838
}
3939

40+
function findAnnotationOrDecoratorNodeInPosition(currentPositionOffset: number, sourceFile: ets.SourceFile): ets.Annotation | ets.Decorator | ets.AnnotationDeclaration | undefined {
41+
let foundNode: ets.Annotation | ets.Decorator | ets.AnnotationDeclaration | undefined
42+
sourceFile.forEachChild(function walk(node): void {
43+
if (!ets.isAnnotation(node) && !ets.isDecorator(node) && !ets.isAnnotationDeclaration(node)) return node.forEachChild(walk)
44+
if (currentPositionOffset >= node.getStart(sourceFile) && currentPositionOffset <= node.getEnd()) {
45+
foundNode = node
46+
return
47+
}
48+
return node.forEachChild(walk)
49+
})
50+
return foundNode
51+
}
52+
4053
function provideAnnotationDeclarationHover(document: TextDocument, currentPositionOffset: number): Hover | undefined {
4154
const languageService = contextUtil.getLanguageService()
4255
if (!languageService) return
@@ -130,9 +143,8 @@ export function patchSemantic(typescriptServices: LanguageServicePlugin[], confi
130143
for (let i = 0; i < encodedClassifications.spans.length; i += 3) {
131144
const offset = encodedClassifications.spans[i] // 第1个值:偏移量
132145
const tsClassification = encodedClassifications.spans[i + 2] // 第3个值:编码的分类
133-
const annotationDeclaration = findNodeInPosition(offset, ets.isAnnotationDeclaration, sourceFile)
134-
const annotation = findNodeInPosition(offset, ets.isDecoratorOrAnnotation, sourceFile)
135-
if ((annotationDeclaration || annotation) && tsClassification === 256) encodedClassifications.spans[i + 2] = 2824
146+
const annotationDeclaration = findAnnotationOrDecoratorNodeInPosition(offset, sourceFile)
147+
if (annotationDeclaration && tsClassification === 256) encodedClassifications.spans[i + 2] = 2824
136148
}
137149
return convertClassificationsToSemanticTokens(document, span, legend, encodedClassifications)
138150
},

0 commit comments

Comments
 (0)