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" ;
3030import { 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
3632export 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