MCPcopy Index your code
hub / github.com/REditorSupport/vscode-R / newDraft

Function newDraft

src/rmarkdown/draft.ts:112–162  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

110}
111
112export async function newDraft(): Promise<void> {
113 const cwd = getCurrentWorkspaceFolder()?.uri.fsPath ?? os.homedir();
114 const template = await launchTemplatePicker(cwd);
115 if (!template) {
116 return;
117 }
118
119 if (template.info.create_dir) {
120 let defaultPath = path.join(cwd, 'draft');
121 let i = 1;
122 while (fs.existsSync(defaultPath)) {
123 defaultPath = path.join(cwd, `draft_${++i}`);
124 }
125 const uri = await window.showSaveDialog({
126 defaultUri: Uri.file(defaultPath),
127 filters: {
128 'Folder': ['']
129 },
130 saveLabel: 'Create Folder',
131 title: 'R Markdown: New Draft'
132 });
133
134 if (uri) {
135 const parsedPath = path.parse(uri.fsPath);
136 const dir = path.join(parsedPath.dir, parsedPath.name);
137 if (fs.existsSync(dir)) {
138 if (await getConfirmation(`Folder already exists. Are you sure you want to replace the folder?`)) {
139 fs.rmdirSync(dir, { recursive: true });
140 } else {
141 return;
142 }
143 }
144
145 const draftPath = await makeDraft(uri.fsPath, template, cwd);
146 if (draftPath) {
147 await workspace.openTextDocument(draftPath)
148 .then(document => window.showTextDocument(document));
149 }
150 }
151 } else {
152 const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'vscode-R-'));
153 const tempFile = path.join(tempDir, 'draft.Rmd');
154 const draftPath = await makeDraft(tempFile, template, cwd);
155 if (draftPath) {
156 const text = fs.readFileSync(draftPath, 'utf8');
157 await workspace.openTextDocument({ language: 'rmd', content: text })
158 .then(document => window.showTextDocument(document));
159 }
160 fs.rmdirSync(tempDir, { recursive: true });
161 }
162}

Callers

nothing calls this directly

Calls 4

getConfirmationFunction · 0.90
launchTemplatePickerFunction · 0.85
makeDraftFunction · 0.85

Tested by

no test coverage detected