From 8f59a20a9ffcfe79940d0cad34dde3311f951887 Mon Sep 17 00:00:00 2001 From: Pratik Gandhi Date: Wed, 1 Sep 2021 21:32:23 +0530 Subject: [PATCH] Add support for loading all the files under a directory --- package.json | 22 ++++++++++++++++++++++ src/evaluate.ts | 35 +++++++++++++++++++++++++++++++++++ src/extension.ts | 4 ++++ 3 files changed, 61 insertions(+) diff --git a/package.json b/package.json index ee79a8613..195fe650b 100644 --- a/package.json +++ b/package.json @@ -884,6 +884,12 @@ "enablement": "calva:connected", "category": "Calva" }, + { + "command": "calva.loadFilesFromDirectory", + "title": "Load all files from a directory", + "enablement": "calva:connected", + "category": "Calva" + }, { "command": "calva.requireREPLUtilities", "title": "Require REPL utilities, like (doc) etcetera, into Current Namespace", @@ -1576,6 +1582,11 @@ "key": "ctrl+alt+c enter", "when": "editorLangId == clojure && calva:connected && calva:keybindingsEnabled" }, + { + "command": "calva.loadFilesFromDirectory", + "key": "", + "when": "editorLangId == clojure && calva:connected && calva:keybindingsEnabled" + }, { "command": "calva.togglePrettyPrint", "key": "ctrl+alt+c p", @@ -2302,6 +2313,11 @@ "command": "calva.loadFile", "group": "calva/b-eval" }, + { + "enablement": "editorLangId == clojure && calva:connected", + "command": "calva.loadFilesFromDirectory", + "group": "calva/b-eval" + }, { "enablement": "editorLangId == clojure && calva:connected", "command": "calva.setOutputWindowNamespace", @@ -2332,6 +2348,12 @@ "command": "calva.loadFile", "group": "calva/b-eval" }, + { + "when": "editorLangId == clojure && calva:showReplUi", + "enablement": "calva:connected", + "command": "calva.loadFilesFromDirectory", + "group": "calva/b-eval" + }, { "when": "editorLangId == clojure && calva:showReplUi", "enablement": "calva:connected", diff --git a/src/evaluate.ts b/src/evaluate.ts index ba735603b..c518d593b 100644 --- a/src/evaluate.ts +++ b/src/evaluate.ts @@ -2,6 +2,7 @@ import * as vscode from 'vscode'; import * as state from './state'; import annotations from './providers/annotations'; import * as path from 'path'; +import * as fs from 'fs'; import * as util from './utilities'; import { NReplSession, NReplEvaluation } from './nrepl'; import statusbar from './statusbar'; @@ -15,6 +16,8 @@ import { getStateValue } from '../out/cljs-lib/cljs-lib'; import { getConfig } from './config'; import * as replSession from './nrepl/repl-session'; import * as getText from './util/get-text'; +import * as projectRoot from './project-root'; +import { file } from 'jszip'; function interruptAllEvaluations() { if (util.getConnectedState()) { @@ -306,6 +309,37 @@ async function loadFile(document, pprintOptions: PrettyPrintingOptions) { } } +function shouldLoadFile(file) { + if ((file.endsWith('clj') || file.endsWith('cljs')) && !file.endsWith('project.clj')) { + return true + } + return false +} + +function walkSync(dir, callback) { + const files = fs.readdirSync(dir); + files.forEach((file) => { + var filepath = path.join(dir, file); + const stats = fs.statSync(filepath); + if (stats.isDirectory()) { + walkSync(filepath, callback); + } else if (stats.isFile()) { + callback(filepath, stats); + } + }); +} + +async function loadFilesFromDirectory(document, pprintOptions: PrettyPrintingOptions) { + const projectRootUri = await projectRoot.getProjectRootUri(); + const projectRootPath = projectRootUri.fsPath; + const directory = await vscode.window.showInputBox({ prompt: 'Enter the directory from which you want to load all the files', + value: projectRootPath }); + + walkSync(directory, async (file) => { + if (shouldLoadFile(file)) + await loadFile({ fileName: file, languageId: 'clojure', uri: vscode.Uri.file(file) }, pprintOptions)}) +} + async function evaluateUser(code: string) { const fileType = util.getFileType(util.getDocument({})), session = replSession.getSession(fileType); @@ -415,6 +449,7 @@ export type customREPLCommandSnippet = { export default { interruptAllEvaluations, loadFile, + loadFilesFromDirectory, evaluateCurrentForm, evaluateEnclosingForm, evaluateTopLevelForm, diff --git a/src/extension.ts b/src/extension.ts index 97543cd8c..ca61bcfe1 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -171,6 +171,10 @@ async function activate(context: vscode.ExtensionContext) { await eval.loadFile({}, config.getConfig().prettyPrintingOptions); outputWindow.appendPrompt(); })); + context.subscriptions.push(vscode.commands.registerCommand('calva.loadFilesFromDirectory', async () => { + await eval.loadFilesFromDirectory({}, config.getConfig().prettyPrintingOptions); + outputWindow.appendPrompt(); + })); context.subscriptions.push(vscode.commands.registerCommand('calva.interruptAllEvaluations', eval.interruptAllEvaluations)); context.subscriptions.push(vscode.commands.registerCommand('calva.evaluateSelection', eval.evaluateCurrentForm)); context.subscriptions.push(vscode.commands.registerCommand('calva.evaluateEnclosingForm', eval.evaluateEnclosingForm));