Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for loading all the files under a directory #1288

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
35 changes: 35 additions & 0 deletions src/evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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()) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -415,6 +449,7 @@ export type customREPLCommandSnippet = {
export default {
interruptAllEvaluations,
loadFile,
loadFilesFromDirectory,
evaluateCurrentForm,
evaluateEnclosingForm,
evaluateTopLevelForm,
Expand Down
4 changes: 4 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down