Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(typescript): fix folding, symbols for TS plugin for .ts #227

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 57 additions & 6 deletions packages/typescript/lib/node/proxyLanguageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
isCodeActionsEnabled,
isCompletionEnabled,
isDefinitionEnabled,
isFoldingRangesEnabled,
isFormattingEnabled,
isHighlightEnabled,
isHoverEnabled,
Expand All @@ -15,6 +16,7 @@ import {
isRenameEnabled,
isSemanticTokensEnabled,
isSignatureHelpEnabled,
isSymbolsEnabled,
isTypeDefinitionEnabled,
} from '@volar/language-core';
import type * as ts from 'typescript';
Expand All @@ -30,6 +32,7 @@ import {
transformDiagnostic,
transformDocumentSpan,
transformFileTextChanges,
transformNavigationTree,
transformSpan,
transformTextChange,
transformTextSpan,
Expand Down Expand Up @@ -109,10 +112,30 @@ export function createProxyLanguageService(languageService: ts.LanguageService)
function getNavigationTree(language: Language<string>, getNavigationTree: ts.LanguageService['getNavigationTree']): ts.LanguageService['getNavigationTree'] {
return filePath => {
const fileName = filePath.replace(windowsPathReg, '/');
const [serviceScript, targetScript] = getServiceScript(language, fileName);
if (serviceScript || targetScript?.associatedOnly) {
const [serviceScript, targetScript, sourceScript] = getServiceScript(language, fileName);
if (serviceScript) {
const tree = getNavigationTree(targetScript.id);
tree.childItems = undefined;
if (targetScript?.associatedOnly) {
tree.childItems = undefined;
return tree;
}
if (
fileName.endsWith('.js')
|| fileName.endsWith('.ts')
|| fileName.endsWith('.jsx')
|| fileName.endsWith('.tsx')
|| fileName.endsWith('.cjs')
|| fileName.endsWith('.cts')
|| fileName.endsWith('.mjs')
|| fileName.endsWith('.mts')
) {
tree.childItems = tree.childItems
?.map(item => transformNavigationTree(sourceScript, language, serviceScript, item, isSymbolsEnabled))
.filter(item => !!item);
}
else {
tree.childItems = undefined;
}
return tree;
}
else {
Expand All @@ -123,9 +146,37 @@ function getNavigationTree(language: Language<string>, getNavigationTree: ts.Lan
function getOutliningSpans(language: Language<string>, getOutliningSpans: ts.LanguageService['getOutliningSpans']): ts.LanguageService['getOutliningSpans'] {
return filePath => {
const fileName = filePath.replace(windowsPathReg, '/');
const [serviceScript, targetScript] = getServiceScript(language, fileName);
if (serviceScript || targetScript?.associatedOnly) {
return [];
const [serviceScript, targetScript, sourceScript] = getServiceScript(language, fileName);
if (serviceScript) {
if (targetScript?.associatedOnly) {
return [];
}
if (
fileName.endsWith('.js')
|| fileName.endsWith('.ts')
|| fileName.endsWith('.jsx')
|| fileName.endsWith('.tsx')
|| fileName.endsWith('.cjs')
|| fileName.endsWith('.cts')
|| fileName.endsWith('.mjs')
|| fileName.endsWith('.mts')
) {
const spans = getOutliningSpans(targetScript.id);
return spans
.map(span => {
const textSpan = transformTextSpan(sourceScript, language, serviceScript, span.textSpan, isFoldingRangesEnabled)?.[1];
if (textSpan) {
return {
...span,
textSpan,
};
}
})
.filter(span => !!span);
}
else {
return [];
}
}
else {
return getOutliningSpans(fileName);
Expand Down
24 changes: 24 additions & 0 deletions packages/typescript/lib/node/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,30 @@ export function transformTextChange(
return undefined;
}

export function transformNavigationTree(
sourceScript: SourceScript<string> | undefined,
language: Language<string>,
serviceScript: TypeScriptServiceScript,
textSpan: ts.NavigationTree,
filter: (data: CodeInformation) => boolean
): ts.NavigationTree {
return {
...textSpan,
spans: textSpan.spans.map(span => {
const [sourceSpanFileName, sourceSpan] = transformTextSpan(sourceScript, language, serviceScript, span, filter) ?? [];
if (sourceSpan && sourceSpanFileName) {
return {
start: sourceSpan.start,
length: sourceSpan.length,
};
}
return span;
}),
childItems: textSpan.childItems?.map(child => transformNavigationTree(sourceScript, language, serviceScript, child, filter)),
nameSpan: textSpan.nameSpan ? transformTextSpan(sourceScript, language, serviceScript, textSpan.nameSpan, filter)?.[1] : undefined,
};
}

export function transformTextSpan(
sourceScript: SourceScript<string> | undefined,
language: Language<string>,
Expand Down
Loading