Skip to content

Commit

Permalink
Consistently clean up vdocs
Browse files Browse the repository at this point in the history
Co-authored-by: Davis Vaughan <[email protected]>
  • Loading branch information
lionel- and DavisVaughan committed Oct 29, 2024
1 parent d7965c2 commit 7abf8f7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 59 deletions.
11 changes: 2 additions & 9 deletions apps/vscode/src/lsp/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ import {
virtualDocUri,
} from "../vdoc/vdoc";
import { activateVirtualDocEmbeddedContent } from "../vdoc/vdoc-content";
import { deactivateVirtualDocTempFiles, isLanguageVirtualDoc } from "../vdoc/vdoc-tempfile";
import { vdocCompletions } from "../vdoc/vdoc-completion";

import {
Expand Down Expand Up @@ -160,8 +159,6 @@ export async function activateLsp(
}

export function deactivate(): Thenable<void> | undefined {
deactivateVirtualDocTempFiles();

if (!client) {
return undefined;
}
Expand Down Expand Up @@ -289,8 +286,7 @@ function embeddedGoToDefinitionProvider(engine: MarkdownEngine) {
adjustedPosition(vdoc.language, position)
);
const resolveLocation = (location: Location) => {
if (isLanguageVirtualDoc(vdoc.language, location.uri) ||
location.uri.toString() === vdocUri.uri.toString()) {
if (location.uri.toString() === vdocUri.uri.toString()) {
return new Location(
document.uri,
unadjustedRange(vdoc.language, location.range)
Expand All @@ -300,8 +296,7 @@ function embeddedGoToDefinitionProvider(engine: MarkdownEngine) {
}
};
const resolveLocationLink = (location: LocationLink) => {
if (isLanguageVirtualDoc(vdoc.language, location.targetUri) ||
location.targetUri.toString() === vdocUri.uri.toString()) {
if (location.targetUri.toString() === vdocUri.uri.toString()) {
const locationLink: LocationLink = {
targetRange: unadjustedRange(vdoc.language, location.targetRange),
originSelectionRange: location.originSelectionRange
Expand Down Expand Up @@ -349,5 +344,3 @@ function isWithinYamlComment(doc: TextDocument, pos: Position) {
const line = doc.lineAt(pos.line).text;
return !!line.match(/^\s*#\s*\| /);
}


69 changes: 19 additions & 50 deletions apps/vscode/src/vdoc/vdoc-tempfile.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* vdoc-tempfile.ts
*
* Copyright (C) 2022 by Posit Software, PBC
* Copyright (C) 2022-2024 by Posit Software, PBC
* Copyright (c) 2019 Takashi Tamura
*
* Unless you have received this program directly from Posit Software pursuant
Expand All @@ -28,42 +28,27 @@ import {
WorkspaceEdit,
} from "vscode";
import { VirtualDoc, VirtualDocUri } from "./vdoc";
import { EmbeddedLanguage } from "./languages";

// one virtual doc per language file extension
const languageVirtualDocs = new Map<String, TextDocument>();

export async function virtualDocUriFromTempFile(
virtualDoc: VirtualDoc,
docPath: string,
local: boolean
): Promise<VirtualDocUri> {
const newVirtualDocUri = (doc: TextDocument) =>
<VirtualDocUri>{
uri: doc.uri,
cleanup: async () => await deleteDocument(doc),
};

// if this is local then create it alongside the docPath and return a cleanup
// function to remove it when the action is completed.
// if this is local then create it alongside the docPath and return a cleanup
// function to remove it when the action is completed.
if (local || virtualDoc.language.localTempFile) {
const ext = virtualDoc.language.extension;
const vdocPath = path.join(path.dirname(docPath), `.vdoc.${ext}`);
fs.writeFileSync(vdocPath, virtualDoc.content);
const vdocUri = Uri.file(vdocPath);
const doc = await workspace.openTextDocument(vdocUri);
return {
uri: doc.uri,
cleanup: async () => await deleteDocument(doc)
};
}

// do we have an existing document?
const langVdoc = languageVirtualDocs.get(virtualDoc.language.extension);
if (langVdoc && !langVdoc.isClosed) {
if (langVdoc.getText() === virtualDoc.content) {
// if its content is identical to what's passed in then just return it
return { uri: langVdoc.uri };
} else {
// otherwise remove it (it will get recreated below)
await deleteDocument(langVdoc);
languageVirtualDocs.delete(virtualDoc.language.extension);
}
return newVirtualDocUri(doc);
}

// write the virtual doc as a temp file
Expand All @@ -72,31 +57,17 @@ export async function virtualDocUriFromTempFile(
// open the document and save a reference to it
const vdocUri = Uri.file(vdocTempFile);
const doc = await workspace.openTextDocument(vdocUri);
languageVirtualDocs.set(virtualDoc.language.extension, doc);

// if this is the first time getting a virtual doc for this
// language then execute a dummy request to cause it to load
if (!langVdoc) {
await commands.executeCommand<Hover[]>(
"vscode.executeHoverProvider",
vdocUri,
new Position(0, 0)
);
}

// return the uri
return { uri: doc.uri };
}

// delete any vdocs left open
export async function deactivateVirtualDocTempFiles() {
languageVirtualDocs.forEach(async (doc) => {
await deleteDocument(doc);
});
}
// TODO: Reevaluate whether this is necessary. Old comment:
// > if this is the first time getting a virtual doc for this
// > language then execute a dummy request to cause it to load
await commands.executeCommand<Hover[]>(
"vscode.executeHoverProvider",
vdocUri,
new Position(0, 0)
);

export function isLanguageVirtualDoc(langauge: EmbeddedLanguage, uri: Uri) {
return languageVirtualDocs.get(langauge.extension)?.uri.toString() === uri.toString();
return newVirtualDocUri(doc);
}

// delete a document
Expand All @@ -122,9 +93,7 @@ function createVirtualDocTempFile(virtualDoc: VirtualDoc) {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
const tmpPath = path.join(
vdocTempDir, ext, ".intellisense." + uuid.v4() + "." + ext
);
const tmpPath = path.join(vdocTempDir, ext, ".intellisense." + uuid.v4() + "." + ext);
fs.writeFileSync(tmpPath, virtualDoc.content);

return tmpPath;
Expand Down

0 comments on commit 7abf8f7

Please sign in to comment.