Skip to content

Commit

Permalink
fix: document features stop working for script block
Browse files Browse the repository at this point in the history
close #1813
  • Loading branch information
johnsoncodehk committed Sep 4, 2022
1 parent 1116427 commit 28b56e0
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 53 deletions.
12 changes: 6 additions & 6 deletions packages/vue-language-service/src/documentFeatures/format.ts
Expand Up @@ -79,16 +79,16 @@ export function register(context: DocumentServiceRuntimeContext) {
let end = sourceMap.getMappedRange(range.end)?.[0].end;

if (!start) {
const minSourceStart = Math.min(...sourceMap.base.mappings.map(m => m.sourceRange.start));
if (document.offsetAt(range.start) <= minSourceStart) {
start = range.start;
const firstMapping = sourceMap.base.mappings.sort((a, b) => a.sourceRange.start - b.sourceRange.start)[0];
if (document.offsetAt(range.start) < firstMapping.sourceRange.start) {
start = sourceMap.mappedDocument.positionAt(firstMapping.mappedRange.start);
}
}

if (!end) {
const maxSourceEnd = Math.max(...sourceMap.base.mappings.map(m => m.sourceRange.end));
if (document.offsetAt(range.end) >= maxSourceEnd) {
end = range.end;
const lastMapping = sourceMap.base.mappings.sort((a, b) => b.sourceRange.start - a.sourceRange.start)[0];
if (document.offsetAt(range.end) > lastMapping.sourceRange.end) {
end = sourceMap.mappedDocument.positionAt(lastMapping.mappedRange.end);
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/vue-language-service/src/documentService.ts
Expand Up @@ -20,7 +20,7 @@ import * as linkedEditingRanges from './documentFeatures/linkedEditingRanges';
import * as selectionRanges from './documentFeatures/selectionRanges';
import { getTsSettings } from './tsConfigs';
import { DocumentServiceRuntimeContext } from './types';
import * as sharedServices from './utils/sharedLs';
import { getSingleFileTypeScriptService } from './utils/singleFileTypeScriptService';
import { parseVueDocument, VueDocument } from './vueDocuments';
import useAutoWrapParenthesesPlugin from './plugins/vue-autoinsert-parentheses';
import useVuePlugin from './plugins/vue';
Expand Down Expand Up @@ -102,7 +102,7 @@ export function getDocumentService(
},
updateTsLs(document) {
if (isTsDocument(document)) {
tsLs = sharedServices.getDummyTsLs(context.typescript, ts2, document, tsSettings);
tsLs = getSingleFileTypeScriptService(context.typescript, ts2, document, tsSettings);
}
},
};
Expand Down
45 changes: 0 additions & 45 deletions packages/vue-language-service/src/utils/sharedLs.ts

This file was deleted.

@@ -0,0 +1,48 @@
import type { TextDocument } from 'vscode-languageserver-textdocument';
import type * as ts2 from '@volar/typescript-language-service';
import { URI } from 'vscode-uri';
import * as shared from '@volar/shared';

let projectVersion = 0;
let doc: TextDocument;
let fileName: string;
let scriptSnapshot: ts.IScriptSnapshot;
let service: ts2.LanguageService | undefined;

const host: ts.LanguageServiceHost = {
readFile: () => undefined,
fileExists: fileName => fileName === fileName,
getProjectVersion: () => projectVersion.toString(),
getScriptVersion: () => projectVersion.toString(),
getCompilationSettings: () => ({ allowJs: true, jsx: 1 }),
getScriptFileNames: () => [fileName],
getScriptSnapshot: fileName => {
if (fileName === fileName) {
return scriptSnapshot;
}
},
getCurrentDirectory: () => '',
getDefaultLibFileName: () => '',
};

export function getSingleFileTypeScriptService(
ts: typeof import('typescript/lib/tsserverlibrary'),
ts2: typeof import('@volar/typescript-language-service'),
_doc: TextDocument,
settings: ts2.Settings,
): ts2.LanguageService {
if (!service) {
service = ts2.createLanguageService(
ts,
host,
ts.createLanguageService(host),
settings,
URI.file('/'),
);
}
projectVersion++;
doc = _doc;
fileName = shared.getPathOfUri(_doc.uri);
scriptSnapshot = ts.ScriptSnapshot.fromString(doc.getText());
return service;
}

0 comments on commit 28b56e0

Please sign in to comment.