volar-service-typescript
Advanced tools
Comparing version 0.0.30 to 0.0.31-patch.1
@@ -1,3 +0,4 @@ | ||
import type { ServicePlugin } from '@volar/language-service'; | ||
import type { Result, ServiceContext, ServicePlugin } from '@volar/language-service'; | ||
import type * as ts from 'typescript'; | ||
import type { TextDocument } from 'vscode-languageserver-textdocument'; | ||
export * from '@volar/typescript'; | ||
@@ -11,3 +12,8 @@ export interface Provide { | ||
} | ||
export declare function create(ts: typeof import('typescript')): ServicePlugin; | ||
export declare function create(ts: typeof import('typescript'), { isFormattingEnabled, isValidationEnabled, isSuggestionsEnabled, isAutoClosingTagsEnabled, }?: { | ||
isFormattingEnabled?(document: TextDocument, context: ServiceContext): Result<boolean>; | ||
isValidationEnabled?(document: TextDocument, context: ServiceContext): Result<boolean>; | ||
isSuggestionsEnabled?(document: TextDocument, context: ServiceContext): Result<boolean>; | ||
isAutoClosingTagsEnabled?(document: TextDocument, context: ServiceContext): Result<boolean>; | ||
}): ServicePlugin; | ||
//# sourceMappingURL=index.d.ts.map |
89
index.js
@@ -20,3 +20,2 @@ "use strict"; | ||
const shared_1 = require("./lib/shared"); | ||
const vscode_uri_1 = require("vscode-uri"); | ||
const typescript_1 = require("@volar/typescript"); | ||
@@ -52,3 +51,11 @@ const tsFaster = require("typescript-auto-import-cache"); | ||
; | ||
function create(ts) { | ||
function create(ts, { isFormattingEnabled = async (document, context) => { | ||
return await context.env.getConfiguration?.((0, shared_1.getConfigTitle)(document) + '.format.enable') ?? true; | ||
}, isValidationEnabled = async (document, context) => { | ||
return await context.env.getConfiguration?.((0, shared_1.getConfigTitle)(document) + '.validate.enable') ?? true; | ||
}, isSuggestionsEnabled = async (document, context) => { | ||
return await context.env.getConfiguration?.((0, shared_1.getConfigTitle)(document) + '.suggest.enabled') ?? true; | ||
}, isAutoClosingTagsEnabled = async (document, context) => { | ||
return await context.env.getConfiguration?.((0, shared_1.getConfigTitle)(document) + '.autoClosingTags') ?? true; | ||
}, } = {}) { | ||
const basicTriggerCharacters = getBasicTriggerCharacters(ts.version); | ||
@@ -75,3 +82,3 @@ const jsDocTriggerCharacter = '*'; | ||
getCompilationSettings: () => ({}), | ||
getCurrentDirectory: () => '/', | ||
getCurrentDirectory: () => '', | ||
getDefaultLibFileName: () => '', | ||
@@ -116,12 +123,10 @@ readFile: () => undefined, | ||
}, | ||
provideAutoInsertionEdit(document, position, lastChange) { | ||
async provideAutoInsertionEdit(document, position, lastChange) { | ||
if ((document.languageId === 'javascriptreact' || document.languageId === 'typescriptreact') | ||
&& lastChange.text.endsWith('>')) { | ||
const config = context.env.getConfiguration?.((0, shared_1.getConfigTitle)(document) + '.autoClosingTags') ?? true; | ||
if (config) { | ||
const ctx = prepareSyntacticService(document); | ||
const close = syntacticCtx.languageService.getJsxClosingTagAtPosition(ctx.fileName, document.offsetAt(position)); | ||
if (close) { | ||
return '$0' + close.newText; | ||
} | ||
&& lastChange.text.endsWith('>') | ||
&& await isAutoClosingTagsEnabled(document, context)) { | ||
const ctx = prepareSyntacticService(document); | ||
const close = syntacticCtx.languageService.getJsxClosingTagAtPosition(ctx.fileName, document.offsetAt(position)); | ||
if (close) { | ||
return '$0' + close.newText; | ||
} | ||
@@ -142,44 +147,18 @@ } | ||
}, | ||
async provideDocumentFormattingEdits(document, range, options_2) { | ||
async provideDocumentFormattingEdits(document, range, options, codeOptions) { | ||
if (!(0, shared_1.isTsDocument)(document)) | ||
return; | ||
const enable = await context.env.getConfiguration?.((0, shared_1.getConfigTitle)(document) + '.format.enable') ?? true; | ||
if (!enable) { | ||
if (!await isFormattingEnabled(document, context)) | ||
return; | ||
} | ||
prepareSyntacticService(document); | ||
return await doFormatting.onRange(document, range, options_2); | ||
return await doFormatting.onRange(document, range, options, codeOptions); | ||
}, | ||
async provideOnTypeFormattingEdits(document, position, key, options_2) { | ||
async provideOnTypeFormattingEdits(document, position, key, options, codeOptions) { | ||
if (!(0, shared_1.isTsDocument)(document)) | ||
return; | ||
const enable = await context.env.getConfiguration?.((0, shared_1.getConfigTitle)(document) + '.format.enable') ?? true; | ||
if (!enable) { | ||
if (!await isFormattingEnabled(document, context)) | ||
return; | ||
} | ||
prepareSyntacticService(document); | ||
return doFormatting.onType(document, options_2, position, key); | ||
return doFormatting.onType(document, options, codeOptions, position, key); | ||
}, | ||
provideFormattingIndentSensitiveLines(document) { | ||
if (!(0, shared_1.isTsDocument)(document)) | ||
return; | ||
const ctx = prepareSyntacticService(document); | ||
const sourceFile = ts.createSourceFile(ctx.fileName, document.getText(), ts.ScriptTarget.ESNext); | ||
if (sourceFile) { | ||
const lines = []; | ||
sourceFile.forEachChild(function walk(node) { | ||
if (node.kind === ts.SyntaxKind.FirstTemplateToken | ||
|| node.kind === ts.SyntaxKind.LastTemplateToken | ||
|| node.kind === ts.SyntaxKind.TemplateHead) { | ||
const startLine = document.positionAt(node.getStart(sourceFile)).line; | ||
const endLine = document.positionAt(node.getEnd()).line; | ||
for (let i = startLine + 1; i <= endLine; i++) { | ||
lines.push(i); | ||
} | ||
} | ||
node.forEachChild(walk); | ||
}); | ||
return lines; | ||
} | ||
}, | ||
}; | ||
@@ -189,2 +168,3 @@ const syntacticHostCtx = { | ||
document: undefined, | ||
documentVersion: undefined, | ||
fileName: '', | ||
@@ -321,6 +301,4 @@ fileVersion: 0, | ||
return; | ||
const enable = await context.env.getConfiguration?.((0, shared_1.getConfigTitle)(document) + '.suggest.enabled') ?? true; | ||
if (!enable) { | ||
if (!await isSuggestionsEnabled(document, context)) | ||
return; | ||
} | ||
return await worker(token, async () => { | ||
@@ -428,6 +406,4 @@ let result = { | ||
return; | ||
const enable = await context.env.getConfiguration?.((0, shared_1.getConfigTitle)(document) + '.validate.enable') ?? true; | ||
if (!enable) { | ||
if (!await isValidationEnabled(document, context)) | ||
return; | ||
} | ||
return await worker(token, () => { | ||
@@ -440,6 +416,4 @@ return doValidation(document.uri, { syntactic: true, suggestion: true }); | ||
return; | ||
const enable = await context.env.getConfiguration?.((0, shared_1.getConfigTitle)(document) + '.validate.enable') ?? true; | ||
if (!enable) { | ||
if (!await isValidationEnabled(document, context)) | ||
return; | ||
} | ||
return worker(token, () => { | ||
@@ -556,6 +530,9 @@ return doValidation(document.uri, { semantic: true, declaration: true }); | ||
function prepareSyntacticService(document) { | ||
if (syntacticHostCtx.document !== document || syntacticHostCtx.fileVersion !== document.version) { | ||
if (syntacticHostCtx.document !== document || syntacticHostCtx.documentVersion !== document.version) { | ||
syntacticHostCtx.document = document; | ||
syntacticHostCtx.fileName = vscode_uri_1.URI.parse(document.uri).fsPath.replace(/\\/g, '/'); | ||
syntacticHostCtx.fileVersion = document.version; | ||
syntacticHostCtx.fileName = '/tmp.' + (document.languageId === 'javascript' ? 'js' : | ||
document.languageId === 'typescriptreact' ? 'tsx' : | ||
document.languageId === 'javascriptreact' ? 'jsx' : | ||
'ts'); | ||
syntacticHostCtx.fileVersion++; | ||
syntacticHostCtx.snapshot = ts.ScriptSnapshot.fromString(document.getText()); | ||
@@ -562,0 +539,0 @@ syntacticHostCtx.projectVersion++; |
@@ -6,4 +6,3 @@ "use strict"; | ||
async function getFormatCodeSettings(ctx, document, options) { | ||
let config = await ctx.env.getConfiguration?.((0, shared_1.getConfigTitle)(document) + '.format'); | ||
config = config ?? {}; | ||
const config = await ctx.env.getConfiguration?.((0, shared_1.getConfigTitle)(document) + '.format') ?? {}; | ||
return { | ||
@@ -13,3 +12,3 @@ convertTabsToSpaces: options?.insertSpaces, | ||
indentSize: options?.tabSize, | ||
indentStyle: 2 /** ts.IndentStyle.Smart */, | ||
indentStyle: 2, | ||
newLineCharacter: '\n', | ||
@@ -16,0 +15,0 @@ insertSpaceAfterCommaDelimiter: config.insertSpaceAfterCommaDelimiter ?? true, |
@@ -5,5 +5,5 @@ import type * as vscode from '@volar/language-service'; | ||
export declare function register(ctx: SharedContext): { | ||
onRange: (document: TextDocument, range: vscode.Range | undefined, options: vscode.FormattingOptions) => Promise<vscode.TextEdit[]>; | ||
onType: (document: TextDocument, options: vscode.FormattingOptions, position: vscode.Position, key: string) => Promise<vscode.TextEdit[]>; | ||
onRange: (document: TextDocument, range: vscode.Range | undefined, options: vscode.FormattingOptions, codeOptions: vscode.EmbeddedCodeFormattingOptions | undefined) => Promise<vscode.TextEdit[]>; | ||
onType: (document: TextDocument, options: vscode.FormattingOptions, codeOptions: vscode.EmbeddedCodeFormattingOptions | undefined, position: vscode.Position, key: string) => Promise<vscode.TextEdit[]>; | ||
}; | ||
//# sourceMappingURL=formatting.d.ts.map |
@@ -8,7 +8,7 @@ "use strict"; | ||
return { | ||
onRange: async (document, range, options) => { | ||
onRange: async (document, range, options, codeOptions) => { | ||
const fileName = ctx.uriToFileName(document.uri); | ||
const tsOptions = await (0, getFormatCodeSettings_1.getFormatCodeSettings)(ctx, document, options); | ||
if (typeof (tsOptions.indentSize) === "boolean" || typeof (tsOptions.indentSize) === "string") { | ||
tsOptions.indentSize = undefined; | ||
if (codeOptions) { | ||
tsOptions.baseIndentSize = codeOptions.initialIndentLevel * options.tabSize; | ||
} | ||
@@ -32,5 +32,8 @@ const scriptEdits = range | ||
}, | ||
onType: async (document, options, position, key) => { | ||
onType: async (document, options, codeOptions, position, key) => { | ||
const fileName = ctx.uriToFileName(document.uri); | ||
const tsOptions = await (0, getFormatCodeSettings_1.getFormatCodeSettings)(ctx, document, options); | ||
if (codeOptions) { | ||
tsOptions.baseIndentSize = codeOptions.initialIndentLevel * options.tabSize; | ||
} | ||
const scriptEdits = (0, shared_1.safeCall)(() => ctx.languageService.getFormattingEditsAfterKeystroke(fileName, document.offsetAt(position), key, tsOptions)); | ||
@@ -37,0 +40,0 @@ if (!scriptEdits) |
@@ -16,10 +16,5 @@ "use strict"; | ||
return; | ||
const response2 = (0, shared_1.safeCall)(() => ctx.languageService.getEncodedSyntacticClassifications(file, { start, length })); | ||
if (!response2) | ||
const response = (0, shared_1.safeCall)(() => ctx.languageService.getEncodedSemanticClassifications(file, { start, length }, ts.SemanticClassificationFormat.TwentyTwenty)); | ||
if (!response) | ||
return; | ||
if (ctx.language.typescript?.languageServiceHost.getCancellationToken?.().isCancellationRequested()) | ||
return; | ||
const response1 = (0, shared_1.safeCall)(() => ctx.languageService.getEncodedSemanticClassifications(file, { start, length }, ts.SemanticClassificationFormat.TwentyTwenty)); | ||
if (!response1) | ||
return; | ||
let tokenModifiersTable = []; | ||
@@ -33,3 +28,3 @@ tokenModifiersTable[2 /* TokenModifier.async */] = 1 << legend.tokenModifiers.indexOf('async'); | ||
tokenModifiersTable = tokenModifiersTable.map(mod => Math.max(mod, 0)); | ||
const tokenSpan = [...response1.spans, ...response2.spans]; | ||
const tokenSpan = response.spans; | ||
const tokens = []; | ||
@@ -41,15 +36,10 @@ let i = 0; | ||
const tsClassification = tokenSpan[i++]; | ||
let tokenModifiers = 0; | ||
let tokenType = getTokenTypeFromClassification(tsClassification); | ||
if (tokenType !== undefined) { | ||
// it's a classification as returned by the typescript-vscode-sh-plugin | ||
tokenModifiers = getTokenModifierFromClassification(tsClassification); | ||
const tokenType = getTokenTypeFromClassification(tsClassification); | ||
if (tokenType === undefined) { | ||
continue; | ||
} | ||
else { | ||
// typescript-vscode-sh-plugin is not present | ||
tokenType = tokenTypeMap[tsClassification]; | ||
if (tokenType === undefined) { | ||
continue; | ||
} | ||
} | ||
const tokenModifiers = getTokenModifierFromClassification(tsClassification); | ||
// we can use the document's range conversion methods because the result is at the same version as the document | ||
const startPos = document.positionAt(offset); | ||
const endPos = document.positionAt(offset + length); | ||
const serverToken = tsTokenTypeToServerTokenType(tokenType); | ||
@@ -60,5 +50,2 @@ if (serverToken === undefined) { | ||
const serverTokenModifiers = tsTokenModifierToServerTokenModifier(tokenModifiers); | ||
// we can use the document's range conversion methods because the result is at the same version as the document | ||
const startPos = document.positionAt(offset); | ||
const endPos = document.positionAt(offset + length); | ||
for (let line = startPos.line; line <= endPos.line; line++) { | ||
@@ -65,0 +52,0 @@ const startCharacter = (line === startPos.line ? startPos.character : 0); |
{ | ||
"name": "volar-service-typescript", | ||
"version": "0.0.30", | ||
"version": "0.0.31-patch.1", | ||
"description": "Integrate TypeScript into Volar", | ||
@@ -36,8 +36,7 @@ "homepage": "https://github.com/volarjs/services/tree/master/packages/typescript", | ||
"vscode-languageserver-textdocument": "^1.0.11", | ||
"vscode-nls": "^5.2.0", | ||
"vscode-uri": "^3.0.8" | ||
"vscode-nls": "^5.2.0" | ||
}, | ||
"peerDependencies": { | ||
"@volar/language-service": "~2.0.1", | ||
"@volar/typescript": "~2.0.1" | ||
"@volar/language-service": "~2.1.0", | ||
"@volar/typescript": "~2.1.0" | ||
}, | ||
@@ -48,4 +47,3 @@ "peerDependenciesMeta": { | ||
} | ||
}, | ||
"gitHead": "30c3cc3c76e90f75f14fe0c2fa4fd33b7ff06507" | ||
} | ||
} |
7
169916
3529
+ Added@volar/language-core@2.1.6(transitive)
+ Added@volar/language-service@2.1.6(transitive)
+ Added@volar/source-map@2.1.6(transitive)
+ Added@volar/typescript@2.1.6(transitive)
- Removedvscode-uri@^3.0.8
- Removed@volar/language-core@2.0.4(transitive)
- Removed@volar/language-service@2.0.4(transitive)
- Removed@volar/source-map@2.0.4(transitive)
- Removed@volar/typescript@2.0.4(transitive)