Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Identify temp versions of documents #181

Merged
merged 2 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
4 changes: 2 additions & 2 deletions src/languageFeatures/rename.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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([
Expand Down
6 changes: 3 additions & 3 deletions src/languageFeatures/updatePastedLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
20 changes: 16 additions & 4 deletions src/types/inMemoryDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Loading