Skip to content

Commit 7abf8f7

Browse files
lionel-DavisVaughan
andcommitted
Consistently clean up vdocs
Co-authored-by: Davis Vaughan <[email protected]>
1 parent d7965c2 commit 7abf8f7

File tree

2 files changed

+21
-59
lines changed

2 files changed

+21
-59
lines changed

apps/vscode/src/lsp/client.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ import {
5353
virtualDocUri,
5454
} from "../vdoc/vdoc";
5555
import { activateVirtualDocEmbeddedContent } from "../vdoc/vdoc-content";
56-
import { deactivateVirtualDocTempFiles, isLanguageVirtualDoc } from "../vdoc/vdoc-tempfile";
5756
import { vdocCompletions } from "../vdoc/vdoc-completion";
5857

5958
import {
@@ -160,8 +159,6 @@ export async function activateLsp(
160159
}
161160

162161
export function deactivate(): Thenable<void> | undefined {
163-
deactivateVirtualDocTempFiles();
164-
165162
if (!client) {
166163
return undefined;
167164
}
@@ -289,8 +286,7 @@ function embeddedGoToDefinitionProvider(engine: MarkdownEngine) {
289286
adjustedPosition(vdoc.language, position)
290287
);
291288
const resolveLocation = (location: Location) => {
292-
if (isLanguageVirtualDoc(vdoc.language, location.uri) ||
293-
location.uri.toString() === vdocUri.uri.toString()) {
289+
if (location.uri.toString() === vdocUri.uri.toString()) {
294290
return new Location(
295291
document.uri,
296292
unadjustedRange(vdoc.language, location.range)
@@ -300,8 +296,7 @@ function embeddedGoToDefinitionProvider(engine: MarkdownEngine) {
300296
}
301297
};
302298
const resolveLocationLink = (location: LocationLink) => {
303-
if (isLanguageVirtualDoc(vdoc.language, location.targetUri) ||
304-
location.targetUri.toString() === vdocUri.uri.toString()) {
299+
if (location.targetUri.toString() === vdocUri.uri.toString()) {
305300
const locationLink: LocationLink = {
306301
targetRange: unadjustedRange(vdoc.language, location.targetRange),
307302
originSelectionRange: location.originSelectionRange
@@ -349,5 +344,3 @@ function isWithinYamlComment(doc: TextDocument, pos: Position) {
349344
const line = doc.lineAt(pos.line).text;
350345
return !!line.match(/^\s*#\s*\| /);
351346
}
352-
353-

apps/vscode/src/vdoc/vdoc-tempfile.ts

Lines changed: 19 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* vdoc-tempfile.ts
33
*
4-
* Copyright (C) 2022 by Posit Software, PBC
4+
* Copyright (C) 2022-2024 by Posit Software, PBC
55
* Copyright (c) 2019 Takashi Tamura
66
*
77
* Unless you have received this program directly from Posit Software pursuant
@@ -28,42 +28,27 @@ import {
2828
WorkspaceEdit,
2929
} from "vscode";
3030
import { VirtualDoc, VirtualDocUri } from "./vdoc";
31-
import { EmbeddedLanguage } from "./languages";
32-
33-
// one virtual doc per language file extension
34-
const languageVirtualDocs = new Map<String, TextDocument>();
3531

3632
export async function virtualDocUriFromTempFile(
3733
virtualDoc: VirtualDoc,
3834
docPath: string,
3935
local: boolean
4036
): Promise<VirtualDocUri> {
37+
const newVirtualDocUri = (doc: TextDocument) =>
38+
<VirtualDocUri>{
39+
uri: doc.uri,
40+
cleanup: async () => await deleteDocument(doc),
41+
};
4142

42-
// if this is local then create it alongside the docPath and return a cleanup
43-
// function to remove it when the action is completed.
43+
// if this is local then create it alongside the docPath and return a cleanup
44+
// function to remove it when the action is completed.
4445
if (local || virtualDoc.language.localTempFile) {
4546
const ext = virtualDoc.language.extension;
4647
const vdocPath = path.join(path.dirname(docPath), `.vdoc.${ext}`);
4748
fs.writeFileSync(vdocPath, virtualDoc.content);
4849
const vdocUri = Uri.file(vdocPath);
4950
const doc = await workspace.openTextDocument(vdocUri);
50-
return {
51-
uri: doc.uri,
52-
cleanup: async () => await deleteDocument(doc)
53-
};
54-
}
55-
56-
// do we have an existing document?
57-
const langVdoc = languageVirtualDocs.get(virtualDoc.language.extension);
58-
if (langVdoc && !langVdoc.isClosed) {
59-
if (langVdoc.getText() === virtualDoc.content) {
60-
// if its content is identical to what's passed in then just return it
61-
return { uri: langVdoc.uri };
62-
} else {
63-
// otherwise remove it (it will get recreated below)
64-
await deleteDocument(langVdoc);
65-
languageVirtualDocs.delete(virtualDoc.language.extension);
66-
}
51+
return newVirtualDocUri(doc);
6752
}
6853

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

77-
// if this is the first time getting a virtual doc for this
78-
// language then execute a dummy request to cause it to load
79-
if (!langVdoc) {
80-
await commands.executeCommand<Hover[]>(
81-
"vscode.executeHoverProvider",
82-
vdocUri,
83-
new Position(0, 0)
84-
);
85-
}
86-
87-
// return the uri
88-
return { uri: doc.uri };
89-
}
90-
91-
// delete any vdocs left open
92-
export async function deactivateVirtualDocTempFiles() {
93-
languageVirtualDocs.forEach(async (doc) => {
94-
await deleteDocument(doc);
95-
});
96-
}
61+
// TODO: Reevaluate whether this is necessary. Old comment:
62+
// > if this is the first time getting a virtual doc for this
63+
// > language then execute a dummy request to cause it to load
64+
await commands.executeCommand<Hover[]>(
65+
"vscode.executeHoverProvider",
66+
vdocUri,
67+
new Position(0, 0)
68+
);
9769

98-
export function isLanguageVirtualDoc(langauge: EmbeddedLanguage, uri: Uri) {
99-
return languageVirtualDocs.get(langauge.extension)?.uri.toString() === uri.toString();
70+
return newVirtualDocUri(doc);
10071
}
10172

10273
// delete a document
@@ -122,9 +93,7 @@ function createVirtualDocTempFile(virtualDoc: VirtualDoc) {
12293
if (!fs.existsSync(dir)) {
12394
fs.mkdirSync(dir);
12495
}
125-
const tmpPath = path.join(
126-
vdocTempDir, ext, ".intellisense." + uuid.v4() + "." + ext
127-
);
96+
const tmpPath = path.join(vdocTempDir, ext, ".intellisense." + uuid.v4() + "." + ext);
12897
fs.writeFileSync(tmpPath, virtualDoc.content);
12998

13099
return tmpPath;

0 commit comments

Comments
 (0)