Skip to content

Commit 1fb466d

Browse files
committed
Improve leiningen project search
I have a problem when I use Calva's Jack In command - I have to run to open my project.clj file in the root of the repo before I Jack In. If I run the command when I have any source files open in the editor, the Jack In fails. The reason for this is that Calva will search from the directory containing the current file down to the workspace root. There are many projects named `project.clj` in the main CircleCI repo, since a project one of our core domain objects: ``` marc@blaster ~/dev/circleci/circle $ find . -name project.clj ./project.clj ./src/circle/project.clj ./src/circle/http/api/v1/v1_0/project.clj ./src/circle/http/api/v1/v1_1/project.clj ./src/circle/http/api/v1/shared/project.clj ./src/circle/http/api/v2/insights/project.clj ./src/circle/http/api/v2/project.clj ./src/circle/http/api/v2/entities/insights/project.clj ./src/circle/http/api/v2/entities/project.clj ./src/circle/model/project.clj ./src/circle/model/api/project.clj ``` If I have any file open in `src/circle`, (which is the location of all source files), Calva will find `src/circle/project.clj` before it considers the project.clj in the root. As far as I understand, this is a desirable search behaviour, since it allows folks to use Calva in projects that contain multiple leiningen projects. This change alters the search behaviour to only consider files that don't begin with the string `(ns ` when attempting the determine the project path. Fixes #871
1 parent 6952885 commit 1fb466d

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/state.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as vscode from 'vscode';
22
import Analytics from './analytics';
33
import * as util from './utilities';
4+
import * as filesCache from './files-cache'
45
import * as path from 'path';
56
import * as os from 'os';
67
import * as fs from 'fs';
@@ -140,14 +141,20 @@ export async function initProjectDir(uri?: vscode.Uri): Promise<void> {
140141
}
141142
}
142143

143-
async function findLocalProjectRoot(projectFileNames, doc, workspaceFolder): Promise<void> {
144+
function isNamespace(path: string): boolean {
145+
const content = filesCache.content(path);
146+
const hasNsDecl = /^\(ns /.test(content);
147+
return hasNsDecl;
148+
}
149+
150+
async function findLocalProjectRoot(projectFileNames: string[], doc: vscode.TextDocument, workspaceFolder: vscode.WorkspaceFolder): Promise<void> {
144151
if (workspaceFolder) {
145152
let rootPath: string = path.resolve(workspaceFolder.uri.fsPath);
146153
setStateValue(PROJECT_DIR_KEY, rootPath);
147154
setStateValue(PROJECT_DIR_URI_KEY, workspaceFolder.uri);
148155

149-
let d = null;
150-
let prev = null;
156+
let d: string = null;
157+
let prev: string = null;
151158
if (doc && path.dirname(doc.uri.fsPath) !== '.') {
152159
d = path.dirname(doc.uri.fsPath);
153160
} else {
@@ -156,7 +163,7 @@ async function findLocalProjectRoot(projectFileNames, doc, workspaceFolder): Pro
156163
while (d !== prev) {
157164
for (let projectFile in projectFileNames) {
158165
const p = path.resolve(d, projectFileNames[projectFile]);
159-
if (fs.existsSync(p)) {
166+
if (fs.existsSync(p) && !isNamespace(p)) {
160167
rootPath = d;
161168
break;
162169
}
@@ -192,8 +199,10 @@ async function findProjectRootUri(projectFileNames, doc, workspaceFolder): Promi
192199
const u = vscode.Uri.joinPath(searchUri, projectFileNames[projectFile]);
193200
try {
194201
await vscode.workspace.fs.stat(u);
195-
setStateValue(PROJECT_DIR_URI_KEY, searchUri);
196-
return;
202+
if (!isNamespace(u.fsPath)) {
203+
setStateValue(PROJECT_DIR_URI_KEY, searchUri);
204+
return;
205+
}
197206
}
198207
catch { }
199208
}

0 commit comments

Comments
 (0)