Skip to content

Commit

Permalink
refactor: rename shared services
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Jun 6, 2021
1 parent 476589b commit 52ce0e4
Show file tree
Hide file tree
Showing 22 changed files with 165 additions and 194 deletions.
4 changes: 2 additions & 2 deletions packages/vscode-vue-languageservice/src/languageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as upath from 'upath';
import type * as ts from 'typescript';
import * as ts2 from 'vscode-typescript-languageservice';
import { DocumentContext, HTMLDocument } from 'vscode-html-languageservice';
import * as languageServices from './utils/languageServices';
import * as sharedLs from './utils/sharedLs';
import { HtmlApiRegisterOptions, TsApiRegisterOptions } from './types';
import { createMapper } from './utils/mapper';
import * as tsPluginApis from './tsPluginApis';
Expand Down Expand Up @@ -75,7 +75,7 @@ export function getDocumentLanguageService({ typescript: ts }: Dependencies) {
return cacheHtmlDoc;
}
}
const htmlDoc = languageServices.html.parseHTMLDocument(document);
const htmlDoc = sharedLs.htmlLs.parseHTMLDocument(document);
cache.set(document.uri, [document.version, htmlDoc]);
return htmlDoc;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/vscode-vue-languageservice/src/parsers/cssClasses.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TextDocument } from 'vscode-languageserver-textdocument';
import * as languageServices from '../utils/languageServices';
import * as sharedLs from '../utils/sharedLs';
import * as css from 'vscode-css-languageservice';

export function parse(
Expand Down Expand Up @@ -39,7 +39,7 @@ export function parse(
}
function findClassNames(doc: TextDocument, ss: css.Stylesheet) {
const result = new Map<string, Set<[number, number]>>();
const cssLanguageService = languageServices.getCssLanguageService(doc.languageId);
const cssLanguageService = sharedLs.getCssLs(doc.languageId);
if (!cssLanguageService) return result;
const symbols = cssLanguageService.findDocumentSymbols(doc, ss);
const usedNodes = new Set<number>();
Expand Down
4 changes: 2 additions & 2 deletions packages/vscode-vue-languageservice/src/services/autoClose.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { HtmlApiRegisterOptions } from '../types';
import type { Position } from 'vscode-languageserver/node';
import type { TextDocument } from 'vscode-languageserver-textdocument';
import * as languageServices from '../utils/languageServices';
import * as sharedLs from '../utils/sharedLs';

export function register({ getHtmlDocument }: HtmlApiRegisterOptions) {
return (document: TextDocument, position: Position): string | undefined | null => {
return languageServices.html.doTagComplete(document, position, getHtmlDocument(document));
return sharedLs.htmlLs.doTagComplete(document, position, getHtmlDocument(document));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from 'vscode-languageserver/node';
import type { SourceFile } from '../sourceFile';
import type { TsApiRegisterOptions } from '../types';
import * as languageServices from '../utils/languageServices';
import * as sharedLs from '../utils/sharedLs';

export function register({ sourceFiles }: TsApiRegisterOptions) {
return (uri: string, color: Color, range: Range) => {
Expand All @@ -19,7 +19,7 @@ export function register({ sourceFiles }: TsApiRegisterOptions) {
function getCssResult(sourceFile: SourceFile) {
let result: ColorPresentation[] = [];
for (const sourceMap of sourceFile.getCssSourceMaps()) {
const cssLanguageService = languageServices.getCssLanguageService(sourceMap.mappedDocument.languageId);
const cssLanguageService = sharedLs.getCssLs(sourceMap.mappedDocument.languageId);
if (!cssLanguageService || !sourceMap.stylesheet) continue;
const cssRanges = sourceMap.getMappedRanges(range.start, range.end);
for (const cssRange of cssRanges) {
Expand Down
16 changes: 8 additions & 8 deletions packages/vscode-vue-languageservice/src/services/completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { CompletionContext, CompletionTriggerKind } from 'vscode-languageserver/
import { SourceFile } from '../sourceFile';
import type { TsApiRegisterOptions } from '../types';
import { CompletionData } from '../types';
import * as languageServices from '../utils/languageServices';
import * as sharedLs from '../utils/sharedLs';
import * as getEmbeddedDocument from './embeddedDocument';

export const triggerCharacter = {
Expand Down Expand Up @@ -362,7 +362,7 @@ export function register({ sourceFiles, tsLanguageService, documentContext, vueH
tags,
globalAttributes,
});
languageServices.html.setDataProviders(true, [dataProvider]);
sharedLs.htmlLs.setDataProviders(true, [dataProvider]);

for (const htmlRange of sourceMap.getMappedRanges(position)) {
if (!result) {
Expand All @@ -372,8 +372,8 @@ export function register({ sourceFiles, tsLanguageService, documentContext, vueH
};
}
const htmlResult = sourceMap.language === 'html'
? await languageServices.html.doComplete2(sourceMap.mappedDocument, htmlRange.start, sourceMap.htmlDocument, documentContext)
: await languageServices.pug.doComplete(sourceMap.pugDocument, htmlRange.start, documentContext)
? await sharedLs.htmlLs.doComplete2(sourceMap.mappedDocument, htmlRange.start, sourceMap.htmlDocument, documentContext)
: await sharedLs.pugLs.doComplete(sourceMap.pugDocument, htmlRange.start, documentContext)
if (!htmlResult) continue;
if (htmlResult.isIncomplete) {
result.isIncomplete = true;
Expand Down Expand Up @@ -489,7 +489,7 @@ export function register({ sourceFiles, tsLanguageService, documentContext, vueH
items: [],
};
}
const cssLanguageService = languageServices.getCssLanguageService(sourceMap.mappedDocument.languageId);
const cssLanguageService = sharedLs.getCssLs(sourceMap.mappedDocument.languageId);
if (!cssLanguageService || !sourceMap.stylesheet) continue;
const wordPattern = wordPatterns[sourceMap.mappedDocument.languageId] ?? wordPatterns.css;
const wordStart = getWordRange(wordPattern, cssRange.end, sourceMap.mappedDocument)?.start; // TODO: use end?
Expand Down Expand Up @@ -532,7 +532,7 @@ export function register({ sourceFiles, tsLanguageService, documentContext, vueH
items: [],
};
}
const jsonLs = languageServices.json;
const jsonLs = sharedLs.jsonLs;
const jsonResult = await jsonLs.doComplete(sourceMap.mappedDocument, cssRange.start, sourceMap.jsonDocument);
if (!jsonResult) continue;
if (jsonResult.isIncomplete) {
Expand All @@ -559,8 +559,8 @@ export function register({ sourceFiles, tsLanguageService, documentContext, vueH
version: 1.1,
tags: vueTags,
});
languageServices.html.setDataProviders(false, [dataProvider]);
return await languageServices.html.doComplete2(sourceFile.getTextDocument(), position, sourceFile.getVueHtmlDocument(), documentContext);
sharedLs.htmlLs.setDataProviders(false, [dataProvider]);
return await sharedLs.htmlLs.doComplete2(sourceFile.getTextDocument(), position, sourceFile.getVueHtmlDocument(), documentContext);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ColorInformation } from 'vscode-languageserver/node';
import type { SourceFile } from '../sourceFile';
import type { TsApiRegisterOptions } from '../types';
import * as languageServices from '../utils/languageServices';
import * as sharedLs from '../utils/sharedLs';

export function register({ sourceFiles }: TsApiRegisterOptions) {
return (uri: string) => {
Expand All @@ -15,7 +15,7 @@ export function register({ sourceFiles }: TsApiRegisterOptions) {
const result: ColorInformation[] = [];
const sourceMaps = sourceFile.getCssSourceMaps();
for (const sourceMap of sourceMaps) {
const cssLanguageService = languageServices.getCssLanguageService(sourceMap.mappedDocument.languageId);
const cssLanguageService = sharedLs.getCssLs(sourceMap.mappedDocument.languageId);
if (!cssLanguageService || !sourceMap.stylesheet) continue;
let colors = cssLanguageService.findDocumentColors(sourceMap.mappedDocument, sourceMap.stylesheet);
for (const color of colors) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { DocumentHighlight, Position } from 'vscode-languageserver/node';
import type { SourceFile } from '../sourceFile';
import type { TsApiRegisterOptions } from '../types';
import * as languageServices from '../utils/languageServices';
import * as sharedLs from '../utils/sharedLs';

export function register({ sourceFiles, tsLanguageService }: TsApiRegisterOptions) {
return (uri: string, position: Position) => {
Expand Down Expand Up @@ -42,8 +42,8 @@ export function register({ sourceFiles, tsLanguageService }: TsApiRegisterOption
for (const htmlRange of sourceMap.getMappedRanges(position)) {

const highlights = sourceMap.language === 'html'
? languageServices.html.findDocumentHighlights(sourceMap.mappedDocument, htmlRange.start, sourceMap.htmlDocument)
: languageServices.pug.findDocumentHighlights(sourceMap.pugDocument, htmlRange.start)
? sharedLs.htmlLs.findDocumentHighlights(sourceMap.mappedDocument, htmlRange.start, sourceMap.htmlDocument)
: sharedLs.pugLs.findDocumentHighlights(sourceMap.pugDocument, htmlRange.start)
if (!highlights) continue;

for (const highlight of highlights) {
Expand All @@ -62,7 +62,7 @@ export function register({ sourceFiles, tsLanguageService }: TsApiRegisterOption
function getCssResult(sourceFile: SourceFile) {
const result: DocumentHighlight[] = [];
for (const sourceMap of sourceFile.getCssSourceMaps()) {
const cssLanguageService = languageServices.getCssLanguageService(sourceMap.mappedDocument.languageId);
const cssLanguageService = sharedLs.getCssLs(sourceMap.mappedDocument.languageId);
if (!cssLanguageService || !sourceMap.stylesheet) continue;
for (const cssRange of sourceMap.getMappedRanges(position)) {
const highlights = cssLanguageService.findDocumentHighlights(sourceMap.mappedDocument, cssRange.start, sourceMap.stylesheet);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { TextDocument } from 'vscode-languageserver-textdocument';
import { DocumentLink, Range } from 'vscode-languageserver/node';
import type { SourceFile } from '../sourceFile';
import type { TsApiRegisterOptions } from '../types';
import * as languageServices from '../utils/languageServices';
import * as sharedLs from '../utils/sharedLs';

export function register({ documentContext, sourceFiles, vueHost }: TsApiRegisterOptions) {
return async (uri: string) => {
Expand Down Expand Up @@ -135,8 +135,8 @@ export function register({ documentContext, sourceFiles, vueHost }: TsApiRegiste
const result: DocumentLink[] = [];
for (const sourceMap of [...sourceFile.getHtmlSourceMaps(), ...sourceFile.getPugSourceMaps()]) {
const links = sourceMap.language === 'html'
? languageServices.html.findDocumentLinks(sourceMap.mappedDocument, documentContext)
: languageServices.pug.findDocumentLinks(sourceMap.pugDocument, documentContext)
? sharedLs.htmlLs.findDocumentLinks(sourceMap.mappedDocument, documentContext)
: sharedLs.pugLs.findDocumentLinks(sourceMap.pugDocument, documentContext)
for (const link of links) {
const vueRange = sourceMap.getSourceRange(link.range.start, link.range.end);
if (vueRange) {
Expand All @@ -153,7 +153,7 @@ export function register({ documentContext, sourceFiles, vueHost }: TsApiRegiste
const sourceMaps = sourceFile.getCssSourceMaps();
const result: DocumentLink[] = [];
for (const sourceMap of sourceMaps) {
const cssLanguageService = languageServices.getCssLanguageService(sourceMap.mappedDocument.languageId);
const cssLanguageService = sharedLs.getCssLs(sourceMap.mappedDocument.languageId);
if (!cssLanguageService || !sourceMap.stylesheet) continue;
const links = await cssLanguageService.findDocumentLinks2(sourceMap.mappedDocument, sourceMap.stylesheet, documentContext);
for (const link of links) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from 'vscode-languageserver/node';
import type { SourceFile } from '../sourceFile';
import type { TsApiRegisterOptions } from '../types';
import * as languageServices from '../utils/languageServices';
import * as sharedLs from '../utils/sharedLs';

export function register({ sourceFiles, tsLanguageService }: TsApiRegisterOptions) {
return (uri: string) => {
Expand Down Expand Up @@ -114,7 +114,7 @@ export function register({ sourceFiles, tsLanguageService }: TsApiRegisterOption
const result: SymbolInformation[] = [];
const sourceMaps = sourceFile.getHtmlSourceMaps();
for (const sourceMap of sourceMaps) {
let symbols = languageServices.html.findDocumentSymbols(sourceMap.mappedDocument, sourceMap.htmlDocument);
let symbols = sharedLs.htmlLs.findDocumentSymbols(sourceMap.mappedDocument, sourceMap.htmlDocument);
if (!symbols) continue;
for (const s of symbols) {
const vueRange = sourceMap.getSourceRange(s.location.range.start, s.location.range.end);
Expand All @@ -132,7 +132,7 @@ export function register({ sourceFiles, tsLanguageService }: TsApiRegisterOption
const result: SymbolInformation[] = [];
const sourceMaps = sourceFile.getCssSourceMaps();
for (const sourceMap of sourceMaps) {
const cssLanguageService = languageServices.getCssLanguageService(sourceMap.mappedDocument.languageId);
const cssLanguageService = sharedLs.getCssLs(sourceMap.mappedDocument.languageId);
if (!cssLanguageService || !sourceMap.stylesheet) continue;
let symbols = cssLanguageService.findDocumentSymbols(sourceMap.mappedDocument, sourceMap.stylesheet);
if (!symbols) continue;
Expand Down
21 changes: 10 additions & 11 deletions packages/vscode-vue-languageservice/src/services/foldingRanges.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import { TextDocument } from 'vscode-languageserver-textdocument';
import type { SourceFile } from '../sourceFile';
import type { SourceMap, TsSourceMap } from '../utils/sourceMaps';
import * as languageServices from '../utils/languageServices';
import * as sharedLs from '../utils/sharedLs';
import { FoldingRangeKind } from 'vscode-css-languageservice';
import { FoldingRange } from 'vscode-languageserver/node';
import { createSourceFile } from '../sourceFile';
import { getCheapTsService2 } from '../utils/languageServices';
import { getDummyTsLs } from '../utils/sharedLs';
import { notEmpty } from '@volar/shared';
import type { HtmlApiRegisterOptions } from '../types';

export function register({ ts }: HtmlApiRegisterOptions) {
return (_document: TextDocument) => {
const tsService2 = getCheapTsService2(ts, _document);
let document = TextDocument.create(tsService2.uri, _document.languageId, _document.version, _document.getText());
let uriTsDocumentMap = new Map();
const sourceFile = createSourceFile(document, tsService2.service, ts, undefined, uriTsDocumentMap);
return (document: TextDocument) => {

let uriTsDocumentMap = new Map();
const dummyTsLs = getDummyTsLs(ts, document);
const sourceFile = createSourceFile(document, dummyTsLs, ts, undefined, uriTsDocumentMap);
const vueResult = getVueResult(sourceFile); // include html folding ranges
const tsResult = getTsResult(sourceFile);
const cssResult = getCssResult(sourceFile);
Expand All @@ -41,7 +40,7 @@ export function register({ ts }: HtmlApiRegisterOptions) {
+ content.split('\n').map(line => ' '.repeat(line.length)).join('\n')
+ docTextWithoutBlocks.substring(block.loc.end);
}
return languageServices.html.getFoldingRanges(TextDocument.create(document.uri, document.languageId, document.version, docTextWithoutBlocks));
return sharedLs.htmlLs.getFoldingRanges(TextDocument.create(document.uri, document.languageId, document.version, docTextWithoutBlocks));
}
function getTsResult(sourceFile: SourceFile) {
const tsSourceMaps = [
Expand All @@ -54,8 +53,8 @@ export function register({ ts }: HtmlApiRegisterOptions) {
for (const sourceMap of tsSourceMaps) {
if (!sourceMap.capabilities.foldingRanges)
continue;
const cheapTs = getCheapTsService2(ts, sourceMap.mappedDocument);
const foldingRanges = cheapTs.service.getFoldingRanges(cheapTs.uri);
const dummyTsLs = getDummyTsLs(ts, sourceMap.mappedDocument);
const foldingRanges = dummyTsLs.getFoldingRanges(sourceMap.mappedDocument.uri);
result = result.concat(toVueFoldingRangesTs(foldingRanges, sourceMap));
}
return result;
Expand All @@ -64,7 +63,7 @@ export function register({ ts }: HtmlApiRegisterOptions) {
let result: FoldingRange[] = [];
for (const sourceMap of sourceFile.getCssSourceMaps()) {
if (!sourceMap.capabilities.foldingRanges) continue;
const cssLanguageService = languageServices.getCssLanguageService(sourceMap.mappedDocument.languageId);
const cssLanguageService = sharedLs.getCssLs(sourceMap.mappedDocument.languageId);
if (!cssLanguageService) continue;
const foldingRanges = cssLanguageService.getFoldingRanges(sourceMap.mappedDocument);
result = result.concat(toVueFoldingRanges(foldingRanges, sourceMap));
Expand Down
16 changes: 7 additions & 9 deletions packages/vscode-vue-languageservice/src/services/formatting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,14 @@ import {
} from 'vscode-languageserver/node';
import { createSourceFile } from '../sourceFile';
import type { HtmlApiRegisterOptions } from '../types';
import * as sharedServices from '../utils/languageServices';
import * as sharedServices from '../utils/sharedLs';

export function register({ ts }: HtmlApiRegisterOptions) {
let uriTsDocumentMap = new Map();
return (_document: TextDocument, options: FormattingOptions) => {
return (document: TextDocument, options: FormattingOptions) => {

const tsService2 = sharedServices.getCheapTsService2(ts, _document);
let document = TextDocument.create(tsService2.uri, _document.languageId, _document.version, _document.getText()); // TODO: high cost

const sourceFile = createSourceFile(document, tsService2.service, ts, undefined, uriTsDocumentMap);
const dummyTsLs = sharedServices.getDummyTsLs(ts, document);
const sourceFile = createSourceFile(document, dummyTsLs, ts, undefined, uriTsDocumentMap);
let newDocument = document;

const pugEdits = getPugFormattingEdits();
Expand Down Expand Up @@ -160,7 +158,7 @@ export function register({ ts }: HtmlApiRegisterOptions) {
function getPugFormattingEdits() {
let result: TextEdit[] = [];
for (const sourceMap of sourceFile.getPugSourceMaps()) {
const pugEdits = sharedServices.pug.format(sourceMap.pugDocument, options);
const pugEdits = sharedServices.pugLs.format(sourceMap.pugDocument, options);
const vueEdits = pugEdits
.map(pugEdit => transformTextEdit(
pugEdit,
Expand All @@ -181,8 +179,8 @@ export function register({ ts }: HtmlApiRegisterOptions) {

for (const sourceMap of tsSourceMaps) {
if (!sourceMap.capabilities.formatting) continue;
const cheapTs = sharedServices.getCheapTsService2(ts, sourceMap.mappedDocument);
const textEdits = cheapTs.service.doFormatting(cheapTs.uri, options);
const dummyTsLs = sharedServices.getDummyTsLs(ts, sourceMap.mappedDocument);
const textEdits = dummyTsLs.doFormatting(sourceMap.mappedDocument.uri, options);
for (const textEdit of textEdits) {
for (const vueRange of sourceMap.getSourceRanges(textEdit.range.start, textEdit.range.end)) {
if (!vueRange.data.capabilities.formatting) continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import type { HtmlApiRegisterOptions } from '../types';
import type { Position } from 'vscode-languageserver/node';
import type { LinkedEditingRanges } from 'vscode-languageserver/node';
import type { TextDocument } from 'vscode-languageserver-textdocument';
import * as languageServices from '../utils/languageServices';
import * as sharedLs from '../utils/sharedLs';

export function register({ getHtmlDocument }: HtmlApiRegisterOptions) {
return (document: TextDocument, position: Position): LinkedEditingRanges | null => {
const ranges = languageServices.html.findLinkedEditingRanges(document, position, getHtmlDocument(document));
const ranges = sharedLs.htmlLs.findLinkedEditingRanges(document, position, getHtmlDocument(document));
if (ranges) {
return { ranges };
}
Expand Down

0 comments on commit 52ce0e4

Please sign in to comment.