volar-service-typescript 0.0.30 → 0.0.31

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
- import type { ServicePlugin } from '@volar/language-service';
1
+ import type { Result, ServiceContext, ServicePlugin } from '@volar/language-service';
2
2
  import type * as ts from 'typescript';
3
+ import type { TextDocument } from 'vscode-languageserver-textdocument';
3
4
  export * from '@volar/typescript';
4
5
  export interface Provide {
5
6
  'typescript/typescript': () => typeof import('typescript');
@@ -8,5 +9,10 @@ export interface Provide {
8
9
  'typescript/syntacticLanguageService': () => ts.LanguageService;
9
10
  'typescript/syntacticLanguageServiceHost': () => ts.LanguageServiceHost;
10
11
  }
11
- export declare function create(ts: typeof import('typescript')): ServicePlugin;
12
+ export declare function create(ts: typeof import('typescript'), { isFormattingEnabled, isValidationEnabled, isSuggestionsEnabled, isAutoClosingTagsEnabled, }?: {
13
+ isFormattingEnabled?(document: TextDocument, context: ServiceContext): Result<boolean>;
14
+ isValidationEnabled?(document: TextDocument, context: ServiceContext): Result<boolean>;
15
+ isSuggestionsEnabled?(document: TextDocument, context: ServiceContext): Result<boolean>;
16
+ isAutoClosingTagsEnabled?(document: TextDocument, context: ServiceContext): Result<boolean>;
17
+ }): ServicePlugin;
12
18
  //# sourceMappingURL=index.d.ts.map
package/index.js CHANGED
@@ -17,7 +17,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  exports.create = void 0;
18
18
  const semver = require("semver");
19
19
  const shared_1 = require("./lib/shared");
20
- const vscode_uri_1 = require("vscode-uri");
21
20
  const typescript_1 = require("@volar/typescript");
22
21
  const tsFaster = require("typescript-auto-import-cache");
23
22
  const _callHierarchy = require("./lib/features/callHierarchy");
@@ -48,7 +47,15 @@ const typeDefinitions = require("./lib/features/typeDefinition");
48
47
  const workspaceSymbols = require("./lib/features/workspaceSymbol");
49
48
  __exportStar(require("@volar/typescript"), exports);
50
49
  ;
51
- function create(ts) {
50
+ function create(ts, { isFormattingEnabled = async (document, context) => {
51
+ return await context.env.getConfiguration?.((0, shared_1.getConfigTitle)(document) + '.format.enable') ?? true;
52
+ }, isValidationEnabled = async (document, context) => {
53
+ return await context.env.getConfiguration?.((0, shared_1.getConfigTitle)(document) + '.validate.enable') ?? true;
54
+ }, isSuggestionsEnabled = async (document, context) => {
55
+ return await context.env.getConfiguration?.((0, shared_1.getConfigTitle)(document) + '.suggest.enabled') ?? true;
56
+ }, isAutoClosingTagsEnabled = async (document, context) => {
57
+ return await context.env.getConfiguration?.((0, shared_1.getConfigTitle)(document) + '.autoClosingTags') ?? true;
58
+ }, } = {}) {
52
59
  const basicTriggerCharacters = getBasicTriggerCharacters(ts.version);
53
60
  const jsDocTriggerCharacter = '*';
54
61
  const directiveCommentTriggerCharacter = '@';
@@ -70,7 +77,7 @@ function create(ts) {
70
77
  getScriptVersion: fileName => fileName === syntacticHostCtx.fileName ? syntacticHostCtx.fileVersion.toString() : '',
71
78
  getScriptSnapshot: fileName => fileName === syntacticHostCtx.fileName ? syntacticHostCtx.snapshot : undefined,
72
79
  getCompilationSettings: () => ({}),
73
- getCurrentDirectory: () => '/',
80
+ getCurrentDirectory: () => '',
74
81
  getDefaultLibFileName: () => '',
75
82
  readFile: () => undefined,
76
83
  fileExists: fileName => fileName === syntacticHostCtx.fileName,
@@ -110,16 +117,14 @@ function create(ts) {
110
117
  'typescript/syntacticLanguageService': () => syntacticCtx.languageService,
111
118
  'typescript/syntacticLanguageServiceHost': () => syntacticCtx.languageServiceHost,
112
119
  },
113
- provideAutoInsertionEdit(document, position, lastChange) {
120
+ async provideAutoInsertionEdit(document, position, lastChange) {
114
121
  if ((document.languageId === 'javascriptreact' || document.languageId === 'typescriptreact')
115
- && lastChange.text.endsWith('>')) {
116
- const config = context.env.getConfiguration?.((0, shared_1.getConfigTitle)(document) + '.autoClosingTags') ?? true;
117
- if (config) {
118
- const ctx = prepareSyntacticService(document);
119
- const close = syntacticCtx.languageService.getJsxClosingTagAtPosition(ctx.fileName, document.offsetAt(position));
120
- if (close) {
121
- return '$0' + close.newText;
122
- }
122
+ && lastChange.text.endsWith('>')
123
+ && await isAutoClosingTagsEnabled(document, context)) {
124
+ const ctx = prepareSyntacticService(document);
125
+ const close = syntacticCtx.languageService.getJsxClosingTagAtPosition(ctx.fileName, document.offsetAt(position));
126
+ if (close) {
127
+ return '$0' + close.newText;
123
128
  }
124
129
  }
125
130
  },
@@ -135,52 +140,27 @@ function create(ts) {
135
140
  prepareSyntacticService(document);
136
141
  return findDocumentSymbols(document.uri);
137
142
  },
138
- async provideDocumentFormattingEdits(document, range, options_2) {
143
+ async provideDocumentFormattingEdits(document, range, options, codeOptions) {
139
144
  if (!(0, shared_1.isTsDocument)(document))
140
145
  return;
141
- const enable = await context.env.getConfiguration?.((0, shared_1.getConfigTitle)(document) + '.format.enable') ?? true;
142
- if (!enable) {
146
+ if (!await isFormattingEnabled(document, context))
143
147
  return;
144
- }
145
148
  prepareSyntacticService(document);
146
- return await doFormatting.onRange(document, range, options_2);
149
+ return await doFormatting.onRange(document, range, options, codeOptions);
147
150
  },
148
- async provideOnTypeFormattingEdits(document, position, key, options_2) {
151
+ async provideOnTypeFormattingEdits(document, position, key, options, codeOptions) {
149
152
  if (!(0, shared_1.isTsDocument)(document))
150
153
  return;
151
- const enable = await context.env.getConfiguration?.((0, shared_1.getConfigTitle)(document) + '.format.enable') ?? true;
152
- if (!enable) {
154
+ if (!await isFormattingEnabled(document, context))
153
155
  return;
154
- }
155
156
  prepareSyntacticService(document);
156
- return doFormatting.onType(document, options_2, position, key);
157
- },
158
- provideFormattingIndentSensitiveLines(document) {
159
- if (!(0, shared_1.isTsDocument)(document))
160
- return;
161
- const ctx = prepareSyntacticService(document);
162
- const sourceFile = ts.createSourceFile(ctx.fileName, document.getText(), ts.ScriptTarget.ESNext);
163
- if (sourceFile) {
164
- const lines = [];
165
- sourceFile.forEachChild(function walk(node) {
166
- if (node.kind === ts.SyntaxKind.FirstTemplateToken
167
- || node.kind === ts.SyntaxKind.LastTemplateToken
168
- || node.kind === ts.SyntaxKind.TemplateHead) {
169
- const startLine = document.positionAt(node.getStart(sourceFile)).line;
170
- const endLine = document.positionAt(node.getEnd()).line;
171
- for (let i = startLine + 1; i <= endLine; i++) {
172
- lines.push(i);
173
- }
174
- }
175
- node.forEachChild(walk);
176
- });
177
- return lines;
178
- }
157
+ return doFormatting.onType(document, options, codeOptions, position, key);
179
158
  },
180
159
  };
181
160
  const syntacticHostCtx = {
182
161
  projectVersion: -1,
183
162
  document: undefined,
163
+ documentVersion: undefined,
184
164
  fileName: '',
185
165
  fileVersion: 0,
186
166
  snapshot: ts.ScriptSnapshot.fromString(''),
@@ -312,10 +292,8 @@ function create(ts) {
312
292
  async provideCompletionItems(document, position, completeContext, token) {
313
293
  if (!isSemanticDocument(document))
314
294
  return;
315
- const enable = await context.env.getConfiguration?.((0, shared_1.getConfigTitle)(document) + '.suggest.enabled') ?? true;
316
- if (!enable) {
295
+ if (!await isSuggestionsEnabled(document, context))
317
296
  return;
318
- }
319
297
  return await worker(token, async () => {
320
298
  let result = {
321
299
  isIncomplete: false,
@@ -418,10 +396,8 @@ function create(ts) {
418
396
  async provideDiagnostics(document, token) {
419
397
  if (!isSemanticDocument(document))
420
398
  return;
421
- const enable = await context.env.getConfiguration?.((0, shared_1.getConfigTitle)(document) + '.validate.enable') ?? true;
422
- if (!enable) {
399
+ if (!await isValidationEnabled(document, context))
423
400
  return;
424
- }
425
401
  return await worker(token, () => {
426
402
  return doValidation(document.uri, { syntactic: true, suggestion: true });
427
403
  });
@@ -429,10 +405,8 @@ function create(ts) {
429
405
  async provideSemanticDiagnostics(document, token) {
430
406
  if (!isSemanticDocument(document))
431
407
  return;
432
- const enable = await context.env.getConfiguration?.((0, shared_1.getConfigTitle)(document) + '.validate.enable') ?? true;
433
- if (!enable) {
408
+ if (!await isValidationEnabled(document, context))
434
409
  return;
435
- }
436
410
  return worker(token, () => {
437
411
  return doValidation(document.uri, { semantic: true, declaration: true });
438
412
  });
@@ -544,10 +518,14 @@ function create(ts) {
544
518
  return result;
545
519
  }
546
520
  function prepareSyntacticService(document) {
547
- if (syntacticHostCtx.document !== document || syntacticHostCtx.fileVersion !== document.version) {
521
+ if (syntacticHostCtx.document !== document || syntacticHostCtx.documentVersion !== document.version) {
548
522
  syntacticHostCtx.document = document;
549
- syntacticHostCtx.fileName = vscode_uri_1.URI.parse(document.uri).fsPath.replace(/\\/g, '/');
550
- syntacticHostCtx.fileVersion = document.version;
523
+ syntacticHostCtx.fileName = '/tmp.' +
524
+ document.languageId === 'javascript' ? 'js' :
525
+ document.languageId === 'typescriptreact' ? 'tsx' :
526
+ document.languageId === 'javascriptreact' ? 'jsx' :
527
+ 'ts';
528
+ syntacticHostCtx.fileVersion++;
551
529
  syntacticHostCtx.snapshot = ts.ScriptSnapshot.fromString(document.getText());
552
530
  syntacticHostCtx.projectVersion++;
553
531
  }
@@ -3,13 +3,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getFormatCodeSettings = void 0;
4
4
  const shared_1 = require("../shared");
5
5
  async function getFormatCodeSettings(ctx, document, options) {
6
- let config = await ctx.env.getConfiguration?.((0, shared_1.getConfigTitle)(document) + '.format');
7
- config = config ?? {};
6
+ const config = await ctx.env.getConfiguration?.((0, shared_1.getConfigTitle)(document) + '.format') ?? {};
8
7
  return {
9
8
  convertTabsToSpaces: options?.insertSpaces,
10
9
  tabSize: options?.tabSize,
11
10
  indentSize: options?.tabSize,
12
- indentStyle: 2 /** ts.IndentStyle.Smart */,
11
+ indentStyle: 2,
13
12
  newLineCharacter: '\n',
14
13
  insertSpaceAfterCommaDelimiter: config.insertSpaceAfterCommaDelimiter ?? true,
15
14
  insertSpaceAfterConstructor: config.insertSpaceAfterConstructor ?? false,
@@ -2,7 +2,7 @@ import type * as vscode from '@volar/language-service';
2
2
  import type { TextDocument } from 'vscode-languageserver-textdocument';
3
3
  import type { SharedContext } from '../types';
4
4
  export declare function register(ctx: SharedContext): {
5
- onRange: (document: TextDocument, range: vscode.Range | undefined, options: vscode.FormattingOptions) => Promise<vscode.TextEdit[]>;
6
- onType: (document: TextDocument, options: vscode.FormattingOptions, position: vscode.Position, key: string) => Promise<vscode.TextEdit[]>;
5
+ onRange: (document: TextDocument, range: vscode.Range | undefined, options: vscode.FormattingOptions, codeOptions: vscode.EmbeddedCodeFormattingOptions | undefined) => Promise<vscode.TextEdit[]>;
6
+ onType: (document: TextDocument, options: vscode.FormattingOptions, codeOptions: vscode.EmbeddedCodeFormattingOptions | undefined, position: vscode.Position, key: string) => Promise<vscode.TextEdit[]>;
7
7
  };
8
8
  //# sourceMappingURL=formatting.d.ts.map
@@ -5,11 +5,11 @@ const getFormatCodeSettings_1 = require("../configs/getFormatCodeSettings");
5
5
  const shared_1 = require("../shared");
6
6
  function register(ctx) {
7
7
  return {
8
- onRange: async (document, range, options) => {
8
+ onRange: async (document, range, options, codeOptions) => {
9
9
  const fileName = ctx.uriToFileName(document.uri);
10
10
  const tsOptions = await (0, getFormatCodeSettings_1.getFormatCodeSettings)(ctx, document, options);
11
- if (typeof (tsOptions.indentSize) === "boolean" || typeof (tsOptions.indentSize) === "string") {
12
- tsOptions.indentSize = undefined;
11
+ if (codeOptions) {
12
+ tsOptions.baseIndentSize = codeOptions.initialIndentLevel * options.tabSize;
13
13
  }
14
14
  const scriptEdits = range
15
15
  ? (0, shared_1.safeCall)(() => ctx.languageService.getFormattingEditsForRange(fileName, document.offsetAt(range.start), document.offsetAt(range.end), tsOptions))
@@ -28,9 +28,12 @@ function register(ctx) {
28
28
  }
29
29
  return result;
30
30
  },
31
- onType: async (document, options, position, key) => {
31
+ onType: async (document, options, codeOptions, position, key) => {
32
32
  const fileName = ctx.uriToFileName(document.uri);
33
33
  const tsOptions = await (0, getFormatCodeSettings_1.getFormatCodeSettings)(ctx, document, options);
34
+ if (codeOptions) {
35
+ tsOptions.baseIndentSize = codeOptions.initialIndentLevel * options.tabSize;
36
+ }
34
37
  const scriptEdits = (0, shared_1.safeCall)(() => ctx.languageService.getFormattingEditsAfterKeystroke(fileName, document.offsetAt(position), key, tsOptions));
35
38
  if (!scriptEdits)
36
39
  return [];
@@ -13,13 +13,8 @@ function register(ctx) {
13
13
  const length = range ? (document.offsetAt(range.end) - start) : document.getText().length;
14
14
  if (ctx.language.typescript?.languageServiceHost.getCancellationToken?.().isCancellationRequested())
15
15
  return;
16
- const response2 = (0, shared_1.safeCall)(() => ctx.languageService.getEncodedSyntacticClassifications(file, { start, length }));
17
- if (!response2)
18
- return;
19
- if (ctx.language.typescript?.languageServiceHost.getCancellationToken?.().isCancellationRequested())
20
- return;
21
- const response1 = (0, shared_1.safeCall)(() => ctx.languageService.getEncodedSemanticClassifications(file, { start, length }, ts.SemanticClassificationFormat.TwentyTwenty));
22
- if (!response1)
16
+ const response = (0, shared_1.safeCall)(() => ctx.languageService.getEncodedSemanticClassifications(file, { start, length }, ts.SemanticClassificationFormat.TwentyTwenty));
17
+ if (!response)
23
18
  return;
24
19
  let tokenModifiersTable = [];
25
20
  tokenModifiersTable[2 /* TokenModifier.async */] = 1 << legend.tokenModifiers.indexOf('async');
@@ -29,34 +24,26 @@ function register(ctx) {
29
24
  tokenModifiersTable[5 /* TokenModifier.local */] = 1 << legend.tokenModifiers.indexOf('local'); // missing in server tokenModifiers
30
25
  tokenModifiersTable[4 /* TokenModifier.defaultLibrary */] = 1 << legend.tokenModifiers.indexOf('defaultLibrary');
31
26
  tokenModifiersTable = tokenModifiersTable.map(mod => Math.max(mod, 0));
32
- const tokenSpan = [...response1.spans, ...response2.spans];
27
+ const tokenSpan = response.spans;
33
28
  const tokens = [];
34
29
  let i = 0;
35
30
  while (i < tokenSpan.length) {
36
31
  const offset = tokenSpan[i++];
37
32
  const length = tokenSpan[i++];
38
33
  const tsClassification = tokenSpan[i++];
39
- let tokenModifiers = 0;
40
- let tokenType = getTokenTypeFromClassification(tsClassification);
41
- if (tokenType !== undefined) {
42
- // it's a classification as returned by the typescript-vscode-sh-plugin
43
- tokenModifiers = getTokenModifierFromClassification(tsClassification);
44
- }
45
- else {
46
- // typescript-vscode-sh-plugin is not present
47
- tokenType = tokenTypeMap[tsClassification];
48
- if (tokenType === undefined) {
49
- continue;
50
- }
34
+ const tokenType = getTokenTypeFromClassification(tsClassification);
35
+ if (tokenType === undefined) {
36
+ continue;
51
37
  }
38
+ const tokenModifiers = getTokenModifierFromClassification(tsClassification);
39
+ // we can use the document's range conversion methods because the result is at the same version as the document
40
+ const startPos = document.positionAt(offset);
41
+ const endPos = document.positionAt(offset + length);
52
42
  const serverToken = tsTokenTypeToServerTokenType(tokenType);
53
43
  if (serverToken === undefined) {
54
44
  continue;
55
45
  }
56
46
  const serverTokenModifiers = tsTokenModifierToServerTokenModifier(tokenModifiers);
57
- // we can use the document's range conversion methods because the result is at the same version as the document
58
- const startPos = document.positionAt(offset);
59
- const endPos = document.positionAt(offset + length);
60
47
  for (let line = startPos.line; line <= endPos.line; line++) {
61
48
  const startCharacter = (line === startPos.line ? startPos.character : 0);
62
49
  const endCharacter = (line === endPos.line ? endPos.character : docLineLength(document, line));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "volar-service-typescript",
3
- "version": "0.0.30",
3
+ "version": "0.0.31",
4
4
  "description": "Integrate TypeScript into Volar",
5
5
  "homepage": "https://github.com/volarjs/services/tree/master/packages/typescript",
6
6
  "bugs": "https://github.com/volarjs/services/issues",
@@ -33,17 +33,16 @@
33
33
  "semver": "^7.5.4",
34
34
  "typescript-auto-import-cache": "^0.3.1",
35
35
  "vscode-languageserver-textdocument": "^1.0.11",
36
- "vscode-nls": "^5.2.0",
37
- "vscode-uri": "^3.0.8"
36
+ "vscode-nls": "^5.2.0"
38
37
  },
39
38
  "peerDependencies": {
40
- "@volar/language-service": "~2.0.1",
41
- "@volar/typescript": "~2.0.1"
39
+ "@volar/language-service": "~2.1.0",
40
+ "@volar/typescript": "~2.1.0"
42
41
  },
43
42
  "peerDependenciesMeta": {
44
43
  "@volar/language-service": {
45
44
  "optional": true
46
45
  }
47
46
  },
48
- "gitHead": "30c3cc3c76e90f75f14fe0c2fa4fd33b7ff06507"
47
+ "gitHead": "f7005aef724767786ee9fe943fa976231cc79bf1"
49
48
  }