This commit is contained in:
2
node_modules/@volar/language-server/lib/utils/combineChangeRanges.d.ts
generated
vendored
Normal file
2
node_modules/@volar/language-server/lib/utils/combineChangeRanges.d.ts
generated
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
import type * as ts from 'typescript';
|
||||
export declare function combineChangeRanges(...changeRanges: ts.TextChangeRange[]): ts.TextChangeRange;
|
||||
28
node_modules/@volar/language-server/lib/utils/combineChangeRanges.js
generated
vendored
Normal file
28
node_modules/@volar/language-server/lib/utils/combineChangeRanges.js
generated
vendored
Normal file
@@ -0,0 +1,28 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.combineChangeRanges = combineChangeRanges;
|
||||
function combineChangeRanges(...changeRanges) {
|
||||
let changeRange = changeRanges[0];
|
||||
for (let i = 1; i < changeRanges.length; i++) {
|
||||
const nextChangeRange = changeRanges[i];
|
||||
changeRange = combineTwoChanges(changeRange, nextChangeRange);
|
||||
}
|
||||
return changeRange;
|
||||
}
|
||||
// https://tsplay.dev/mMldVN - @browsnet
|
||||
function combineTwoChanges(a, b) {
|
||||
const aStart = a.span.start;
|
||||
const aEnd = a.span.start + a.span.length;
|
||||
const aDiff = a.newLength - a.span.length;
|
||||
const changeBegin = aStart + Math.min(a.span.length, a.newLength);
|
||||
const rollback = (start) => start > changeBegin ? Math.max(aStart, start - aDiff) : start;
|
||||
const bStart = rollback(b.span.start);
|
||||
const bEnd = rollback(b.span.start + b.span.length);
|
||||
const bDiff = b.newLength - b.span.length;
|
||||
const start = Math.min(aStart, bStart);
|
||||
const end = Math.max(aEnd, bEnd);
|
||||
const length = end - start;
|
||||
const newLength = aDiff + bDiff + length;
|
||||
return { span: { start, length }, newLength };
|
||||
}
|
||||
//# sourceMappingURL=combineChangeRanges.js.map
|
||||
24
node_modules/@volar/language-server/lib/utils/snapshotDocument.d.ts
generated
vendored
Normal file
24
node_modules/@volar/language-server/lib/utils/snapshotDocument.d.ts
generated
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
import type * as ts from 'typescript';
|
||||
import type * as vscode from 'vscode-languageserver-protocol';
|
||||
import { Range, TextDocument } from 'vscode-languageserver-textdocument';
|
||||
export declare class SnapshotDocument implements TextDocument {
|
||||
private document;
|
||||
private snapshots;
|
||||
constructor(uri: string, languageId: string, version: number, text: string);
|
||||
get uri(): string;
|
||||
get languageId(): string;
|
||||
get version(): number;
|
||||
get lineCount(): number;
|
||||
getText(range?: Range): string;
|
||||
positionAt(offset: number): import("vscode-languageserver-textdocument").Position;
|
||||
offsetAt(position: vscode.Position): number;
|
||||
/**
|
||||
* Update the document with the given content changes and version.
|
||||
* If all changes is incremental, calculate the change range and add a new snapshot.
|
||||
* Otherwise, reset the changes.
|
||||
*/
|
||||
update(contentChanges: vscode.TextDocumentContentChangeEvent[], version: number): void;
|
||||
getSnapshot(): ts.IScriptSnapshot;
|
||||
private resetChanges;
|
||||
private clearUnreferencedVersions;
|
||||
}
|
||||
120
node_modules/@volar/language-server/lib/utils/snapshotDocument.js
generated
vendored
Normal file
120
node_modules/@volar/language-server/lib/utils/snapshotDocument.js
generated
vendored
Normal file
@@ -0,0 +1,120 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.SnapshotDocument = void 0;
|
||||
const vscode_languageserver_textdocument_1 = require("vscode-languageserver-textdocument");
|
||||
const combineChangeRanges_1 = require("./combineChangeRanges");
|
||||
class SnapshotDocument {
|
||||
constructor(uri, languageId, version, text) {
|
||||
this.snapshots = [];
|
||||
this.document = vscode_languageserver_textdocument_1.TextDocument.create(uri, languageId, version, text);
|
||||
this.resetChanges();
|
||||
}
|
||||
get uri() {
|
||||
return this.document.uri;
|
||||
}
|
||||
get languageId() {
|
||||
return this.document.languageId;
|
||||
}
|
||||
get version() {
|
||||
return this.document.version;
|
||||
}
|
||||
get lineCount() {
|
||||
return this.document.lineCount;
|
||||
}
|
||||
getText(range) {
|
||||
return this.document.getText(range);
|
||||
}
|
||||
positionAt(offset) {
|
||||
return this.document.positionAt(offset);
|
||||
}
|
||||
offsetAt(position) {
|
||||
return this.document.offsetAt(position);
|
||||
}
|
||||
/**
|
||||
* Update the document with the given content changes and version.
|
||||
* If all changes is incremental, calculate the change range and add a new snapshot.
|
||||
* Otherwise, reset the changes.
|
||||
*/
|
||||
update(contentChanges, version) {
|
||||
if (contentChanges.every(change => 'range' in change)) {
|
||||
let changeRanges = [];
|
||||
for (const contentChange of contentChanges) {
|
||||
if (!('range' in contentChange)) {
|
||||
continue;
|
||||
}
|
||||
const start = this.offsetAt(contentChange.range.start);
|
||||
const length = contentChange.rangeLength ?? this.offsetAt(contentChange.range.end) - start;
|
||||
changeRanges.push({
|
||||
span: { start, length },
|
||||
newLength: contentChange.text.length
|
||||
});
|
||||
vscode_languageserver_textdocument_1.TextDocument.update(this.document, [contentChange], version);
|
||||
}
|
||||
this.snapshots.push({
|
||||
changeRange: (0, combineChangeRanges_1.combineChangeRanges)(...changeRanges),
|
||||
version,
|
||||
ref: undefined,
|
||||
});
|
||||
}
|
||||
else {
|
||||
vscode_languageserver_textdocument_1.TextDocument.update(this.document, contentChanges, version);
|
||||
this.resetChanges();
|
||||
}
|
||||
}
|
||||
getSnapshot() {
|
||||
this.clearUnreferencedVersions();
|
||||
const lastChange = this.snapshots[this.snapshots.length - 1];
|
||||
if (!lastChange.ref) {
|
||||
const text = this.document.getText();
|
||||
const changeRangeCache = new WeakMap();
|
||||
const snapshot = {
|
||||
getText: (start, end) => text.substring(start, end),
|
||||
getLength: () => text.length,
|
||||
getChangeRange: oldSnapshot => {
|
||||
if (!changeRangeCache.has(oldSnapshot)) {
|
||||
const oldIndex = this.snapshots.findIndex(change => change.ref?.deref() === oldSnapshot);
|
||||
if (oldIndex >= 0) {
|
||||
const start = oldIndex + 1;
|
||||
const end = this.snapshots.indexOf(lastChange) + 1;
|
||||
const changeRanges = this.snapshots
|
||||
.slice(start, end)
|
||||
.map(change => change.changeRange);
|
||||
const changeRange = (0, combineChangeRanges_1.combineChangeRanges)(...changeRanges);
|
||||
changeRangeCache.set(oldSnapshot, changeRange);
|
||||
}
|
||||
else {
|
||||
changeRangeCache.set(oldSnapshot, undefined);
|
||||
}
|
||||
}
|
||||
return changeRangeCache.get(oldSnapshot);
|
||||
},
|
||||
};
|
||||
lastChange.ref = new WeakRef(snapshot);
|
||||
}
|
||||
return lastChange.ref.deref();
|
||||
}
|
||||
resetChanges() {
|
||||
this.snapshots = [
|
||||
{
|
||||
changeRange: {
|
||||
span: {
|
||||
start: 0,
|
||||
length: 0,
|
||||
},
|
||||
newLength: this.document.getText().length,
|
||||
},
|
||||
version: this.document.version,
|
||||
ref: undefined,
|
||||
}
|
||||
];
|
||||
}
|
||||
clearUnreferencedVersions() {
|
||||
let firstReferencedIndex = 0;
|
||||
while (firstReferencedIndex < this.snapshots.length - 1 && !this.snapshots[firstReferencedIndex].ref?.deref()) {
|
||||
firstReferencedIndex++;
|
||||
}
|
||||
this.snapshots = this.snapshots.slice(firstReferencedIndex);
|
||||
}
|
||||
}
|
||||
exports.SnapshotDocument = SnapshotDocument;
|
||||
//# sourceMappingURL=snapshotDocument.js.map
|
||||
Reference in New Issue
Block a user