@volar/language-server 2.4.0-alpha.2 → 2.4.0-alpha.20

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.
@@ -0,0 +1,28 @@
1
+ import type * as ts from 'typescript';
2
+ import type * as vscode from 'vscode-languageserver-protocol';
3
+ import { Range, TextDocument } from 'vscode-languageserver-textdocument';
4
+ export declare class SnapshotDocument implements TextDocument {
5
+ private document;
6
+ private snapshots;
7
+ constructor(uri: string, languageId: string, version: number, text: string);
8
+ get uri(): string;
9
+ get languageId(): string;
10
+ get version(): number;
11
+ get lineCount(): number;
12
+ getText(range?: Range): string;
13
+ positionAt(offset: number): import("vscode-languageserver-textdocument").Position;
14
+ offsetAt(position: vscode.Position): number;
15
+ /**
16
+ * Update the document with the given content changes and version.
17
+ * If all changes is incremental, calculate the change range and add a new snapshot.
18
+ * Otherwise, reset the changes.
19
+ */
20
+ update(contentChanges: vscode.TextDocumentContentChangeEvent[], version: number): void;
21
+ getSnapshot(): ts.IScriptSnapshot;
22
+ private resetChanges;
23
+ /**
24
+ * Calculate the change range from the given content changes.
25
+ */
26
+ private calculateChangeRange;
27
+ private clearUnreferencedVersions;
28
+ }
@@ -0,0 +1,138 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SnapshotDocument = void 0;
4
+ const vscode_languageserver_textdocument_1 = require("vscode-languageserver-textdocument");
5
+ const combineChangeRanges_1 = require("./combineChangeRanges");
6
+ class SnapshotDocument {
7
+ constructor(uri, languageId, version, text) {
8
+ this.snapshots = [];
9
+ this.document = vscode_languageserver_textdocument_1.TextDocument.create(uri, languageId, version, text);
10
+ this.resetChanges();
11
+ }
12
+ get uri() {
13
+ return this.document.uri;
14
+ }
15
+ get languageId() {
16
+ return this.document.languageId;
17
+ }
18
+ get version() {
19
+ return this.document.version;
20
+ }
21
+ get lineCount() {
22
+ return this.document.lineCount;
23
+ }
24
+ getText(range) {
25
+ return this.document.getText(range);
26
+ }
27
+ positionAt(offset) {
28
+ return this.document.positionAt(offset);
29
+ }
30
+ offsetAt(position) {
31
+ return this.document.offsetAt(position);
32
+ }
33
+ /**
34
+ * Update the document with the given content changes and version.
35
+ * If all changes is incremental, calculate the change range and add a new snapshot.
36
+ * Otherwise, reset the changes.
37
+ */
38
+ update(contentChanges, version) {
39
+ if (contentChanges.every(change => 'range' in change)) {
40
+ const { minStart, oldLength, lengthDiff } = this.calculateChangeRange(contentChanges);
41
+ vscode_languageserver_textdocument_1.TextDocument.update(this.document, contentChanges, version);
42
+ this.snapshots.push({
43
+ changeRange: {
44
+ span: {
45
+ start: minStart,
46
+ length: oldLength,
47
+ },
48
+ newLength: oldLength + lengthDiff,
49
+ },
50
+ version,
51
+ ref: undefined,
52
+ });
53
+ }
54
+ else {
55
+ vscode_languageserver_textdocument_1.TextDocument.update(this.document, contentChanges, version);
56
+ this.resetChanges();
57
+ }
58
+ }
59
+ getSnapshot() {
60
+ this.clearUnreferencedVersions();
61
+ const lastChange = this.snapshots[this.snapshots.length - 1];
62
+ if (!lastChange.ref) {
63
+ const text = this.document.getText();
64
+ const changeRangeCache = new WeakMap();
65
+ const snapshot = {
66
+ getText: (start, end) => text.substring(start, end),
67
+ getLength: () => text.length,
68
+ getChangeRange: oldSnapshot => {
69
+ if (!changeRangeCache.has(oldSnapshot)) {
70
+ const oldIndex = this.snapshots.findIndex(change => change.ref?.deref() === oldSnapshot);
71
+ if (oldIndex >= 0) {
72
+ const start = oldIndex + 1;
73
+ const end = this.snapshots.indexOf(lastChange) + 1;
74
+ const changeRanges = this.snapshots
75
+ .slice(start, end)
76
+ .map(change => change.changeRange);
77
+ const changeRange = (0, combineChangeRanges_1.combineChangeRanges)(...changeRanges);
78
+ changeRangeCache.set(oldSnapshot, changeRange);
79
+ }
80
+ else {
81
+ changeRangeCache.set(oldSnapshot, undefined);
82
+ }
83
+ }
84
+ return changeRangeCache.get(oldSnapshot);
85
+ },
86
+ };
87
+ lastChange.ref = new WeakRef(snapshot);
88
+ }
89
+ return lastChange.ref.deref();
90
+ }
91
+ resetChanges() {
92
+ this.snapshots = [
93
+ {
94
+ changeRange: {
95
+ span: {
96
+ start: 0,
97
+ length: 0,
98
+ },
99
+ newLength: this.document.getText().length,
100
+ },
101
+ version: this.document.version,
102
+ ref: undefined,
103
+ }
104
+ ];
105
+ }
106
+ /**
107
+ * Calculate the change range from the given content changes.
108
+ */
109
+ calculateChangeRange(contentChanges) {
110
+ let lengthDiff = 0;
111
+ const starts = [];
112
+ const ends = [];
113
+ for (const contentChange of contentChanges) {
114
+ if (!('range' in contentChange)) {
115
+ continue;
116
+ }
117
+ const start = this.offsetAt(contentChange.range.start);
118
+ const length = contentChange.rangeLength ?? this.offsetAt(contentChange.range.end) - start;
119
+ const end = start + length;
120
+ starts.push(start);
121
+ ends.push(end);
122
+ lengthDiff += contentChange.text.length - length;
123
+ }
124
+ const minStart = Math.min(...starts);
125
+ const maxEnd = Math.max(...ends);
126
+ const oldLength = maxEnd - minStart;
127
+ return { minStart, oldLength, lengthDiff };
128
+ }
129
+ clearUnreferencedVersions() {
130
+ let firstReferencedIndex = 0;
131
+ while (firstReferencedIndex < this.snapshots.length - 1 && !this.snapshots[firstReferencedIndex].ref?.deref()) {
132
+ firstReferencedIndex++;
133
+ }
134
+ this.snapshots = this.snapshots.slice(firstReferencedIndex);
135
+ }
136
+ }
137
+ exports.SnapshotDocument = SnapshotDocument;
138
+ //# sourceMappingURL=snapshotDocument.js.map
package/node.js CHANGED
@@ -88,6 +88,7 @@ exports.fs = {
88
88
  },
89
89
  };
90
90
  function loadTsdkByPath(tsdk, locale) {
91
+ locale = locale?.toLowerCase();
91
92
  // webpack compatibility
92
93
  const _require = eval('require');
93
94
  return {
@@ -111,6 +112,9 @@ function loadTsdkByPath(tsdk, locale) {
111
112
  throw new Error(`Can't find typescript.js or tsserverlibrary.js in ${JSON.stringify(tsdk)}`);
112
113
  }
113
114
  function loadLocalizedDiagnosticMessages() {
115
+ if (locale === 'en') {
116
+ return;
117
+ }
114
118
  try {
115
119
  const path = _require.resolve(`./${locale}/diagnosticMessages.generated.json`, { paths: [tsdk] });
116
120
  return _require(path);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@volar/language-server",
3
- "version": "2.4.0-alpha.2",
3
+ "version": "2.4.0-alpha.20",
4
4
  "license": "MIT",
5
5
  "files": [
6
6
  "**/*.js",
@@ -12,10 +12,10 @@
12
12
  "directory": "packages/language-server"
13
13
  },
14
14
  "dependencies": {
15
- "@volar/language-core": "2.4.0-alpha.2",
16
- "@volar/language-service": "2.4.0-alpha.2",
17
- "@volar/snapshot-document": "2.4.0-alpha.2",
18
- "@volar/typescript": "2.4.0-alpha.2",
15
+ "@volar/language-core": "2.4.0-alpha.20",
16
+ "@volar/language-service": "2.4.0-alpha.20",
17
+ "@volar/snapshot-document": "2.4.0-alpha.20",
18
+ "@volar/typescript": "2.4.0-alpha.20",
19
19
  "path-browserify": "^1.0.1",
20
20
  "request-light": "^0.7.0",
21
21
  "vscode-languageserver": "^9.0.1",
@@ -26,5 +26,5 @@
26
26
  "devDependencies": {
27
27
  "@types/path-browserify": "latest"
28
28
  },
29
- "gitHead": "62914047f925b79391fee6e4f697705b77641baf"
29
+ "gitHead": "63ec0dfd91333c91a7c6443cedc1ae074b317867"
30
30
  }