diff --git a/CHANGELOG.md b/CHANGELOG.md index a319fdb..eddc07c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +## 0.5.0-alpha.6 — April 25, 2024 +- Clearly identify temporary versions of documents by setting version to `-1`. This lets clients know not to cache them. + ## 0.5.0-alpha.5 — April 5, 2024 - Add links to open file in path completions. - Add previews for image and video files in path completions. diff --git a/package.json b/package.json index c7b81ee..d48c1e6 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "vscode-markdown-languageservice", "description": "Markdown language service", - "version": "0.5.0-alpha.5", + "version": "0.5.0-alpha.6", "author": "Microsoft Corporation", "license": "MIT", "engines": { diff --git a/src/languageFeatures/rename.ts b/src/languageFeatures/rename.ts index 6d32b5c..23a51ec 100644 --- a/src/languageFeatures/rename.ts +++ b/src/languageFeatures/rename.ts @@ -12,7 +12,7 @@ import { IMdParser } from '../parser'; import { ISlugifier } from '../slugify'; import { MdTableOfContentsProvider, TableOfContents, TocEntry } from '../tableOfContents'; import { HrefKind, InternalHref, MdLink, MdLinkKind, MdLinkSource } from '../types/documentLink'; -import { InMemoryDocument } from '../types/inMemoryDocument'; +import { InMemoryDocument, tempDocVersion } from '../types/inMemoryDocument'; import { arePositionsEqual, translatePosition } from '../types/position'; import { modifyRange, rangeContains } from '../types/range'; import { ITextDocument, getDocUri } from '../types/textDocument'; @@ -227,7 +227,7 @@ export class MdRenameProvider { } if (doc) { - const editedDoc = new InMemoryDocument(URI.parse(existingHeader.location.uri), doc.getText()) + const editedDoc = new InMemoryDocument(URI.parse(existingHeader.location.uri), doc.getText(), tempDocVersion) .applyEdits([lsp.TextEdit.replace(existingHeader.location.range, '# ' + newHeaderText)]); const [oldToc, newToc] = await Promise.all([ diff --git a/src/languageFeatures/updatePastedLinks.ts b/src/languageFeatures/updatePastedLinks.ts index 0ff32a5..3d75554 100644 --- a/src/languageFeatures/updatePastedLinks.ts +++ b/src/languageFeatures/updatePastedLinks.ts @@ -6,7 +6,7 @@ import * as lsp from 'vscode-languageserver-protocol'; import { URI } from 'vscode-uri'; import { LsConfiguration } from '../config'; import { HrefKind, LinkDefinitionSet, MdLinkDefinition } from '../types/documentLink'; -import { InMemoryDocument } from '../types/inMemoryDocument'; +import { InMemoryDocument, tempDocVersion } from '../types/inMemoryDocument'; import { isBefore, isBeforeOrEqual } from '../types/position'; import { rangeContains } from '../types/range'; import { getDocUri, ITextDocument } from '../types/textDocument'; @@ -83,7 +83,7 @@ export class MdUpdatePastedLinksProvider { // Find the links in the pasted text by applying the paste edits to an in-memory document. // Use `copySource` as the doc uri to make sure links are resolved in its context - const editedDoc = new InMemoryDocument(metadata.source, targetDocument.getText()); + const editedDoc = new InMemoryDocument(metadata.source, targetDocument.getText(), tempDocVersion); editedDoc.replaceContents(editedDoc.previewEdits(sortedPastes)); const allLinks = await this.#linkProvider.getLinksWithoutCaching(editedDoc, token); @@ -206,7 +206,7 @@ export class MdUpdatePastedLinksProvider { } } - #computedPastedRanges(sortedPastes: lsp.TextEdit[], targetDocument: ITextDocument, editedDoc: InMemoryDocument) { + #computedPastedRanges(sortedPastes: readonly lsp.TextEdit[], targetDocument: ITextDocument, editedDoc: InMemoryDocument): lsp.Range[] { const pastedRanges: lsp.Range[] = []; let offsetAdjustment = 0; diff --git a/src/types/inMemoryDocument.ts b/src/types/inMemoryDocument.ts index b478615..7241ca1 100644 --- a/src/types/inMemoryDocument.ts +++ b/src/types/inMemoryDocument.ts @@ -8,6 +8,13 @@ import { TextDocument } from 'vscode-languageserver-textdocument'; import { URI } from 'vscode-uri'; import { ITextDocument } from './textDocument'; +/** + * Represents a temporary version of a document. + * + * This indicates that the document should not be cached or reuse cached results. + */ +export const tempDocVersion = -1; + export class InMemoryDocument implements ITextDocument { #doc: TextDocument; @@ -43,18 +50,23 @@ export class InMemoryDocument implements ITextDocument { } replaceContents(newContent: string): this { - ++this.version; - TextDocument.update(this.#doc, [{ text: newContent }], this.version); + this.#update([{ text: newContent }]); return this; } applyEdits(textEdits: readonly lsp.TextEdit[]): this { - ++this.version; - TextDocument.update(this.#doc, textEdits.map(x => ({ range: x.range, text: x.newText })), this.version); + this.#update(textEdits.map(x => ({ range: x.range, text: x.newText }))); return this; } previewEdits(textEdits: lsp.TextEdit[]): string { return TextDocument.applyEdits(this.#doc, textEdits); } + + #update(changes: lsp.TextDocumentContentChangeEvent[]) { + // Temp docs always share the same version + const newVersion = this.version < 0 ? this.version : this.version + 1; + this.version = newVersion; + TextDocument.update(this.#doc, changes, newVersion); + } }