Skip to content
This repository was archived by the owner on Nov 18, 2022. It is now read-only.

Commit e414e6a

Browse files
authored
Merge pull request #457 from jannickj/remove-weird-cargo-must-be-in-root-limitation
search for the cargo toml based on the current file
2 parents 1811138 + ba47447 commit e414e6a

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

src/extension.ts

+27-11
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { activateTaskProvider, runCommand } from './tasks';
1717

1818
import * as child_process from 'child_process';
1919
import * as fs from 'fs';
20+
import path = require('path');
2021

2122
import { commands, ExtensionContext, IndentAction, languages, TextEditor,
2223
TextEditorEdit, window, workspace, TextDocument, WorkspaceFolder, Disposable, Uri,
@@ -53,6 +54,11 @@ function didOpenTextDocument(document: TextDocument, context: ExtensionContext):
5354
return;
5455
}
5556
folder = getOuterMostWorkspaceFolder(folder);
57+
folder = getCargoTomlWorkspace(folder, document.uri.fsPath);
58+
if (!folder) {
59+
stopSpinner(`RLS: Cargo.toml missing`);
60+
return;
61+
}
5662

5763
if (!workspaces.has(folder.uri.toString())) {
5864
const workspace = new ClientWorkspace(folder);
@@ -82,6 +88,27 @@ function sortedWorkspaceFolders(): string[] {
8288
return _sortedWorkspaceFolders || [];
8389
}
8490

91+
function getCargoTomlWorkspace(cur_workspace: WorkspaceFolder, file_path: string): WorkspaceFolder | undefined {
92+
let current = file_path;
93+
const workspace_root = path.parse(cur_workspace.uri.fsPath).dir;
94+
while (true) {
95+
const old = current;
96+
current = path.dirname(current);
97+
if (old == current) {
98+
break;
99+
}
100+
if (workspace_root == path.parse(current).dir) {
101+
break;
102+
}
103+
104+
const cargo_path = path.join(current, 'Cargo.toml');
105+
if (fs.existsSync(cargo_path)) {
106+
return { ...cur_workspace, uri: Uri.parse(current) };
107+
}
108+
}
109+
return undefined;
110+
}
111+
85112
function getOuterMostWorkspaceFolder(folder: WorkspaceFolder): WorkspaceFolder {
86113
const sorted = sortedWorkspaceFolders();
87114
for (const element of sorted) {
@@ -146,8 +173,6 @@ class ClientWorkspace {
146173
}
147174

148175
async start(context: ExtensionContext) {
149-
// These methods cannot throw an error, so we can drop it.
150-
warnOnMissingCargoToml();
151176

152177
startSpinner('RLS', 'Starting');
153178

@@ -436,15 +461,6 @@ class ClientWorkspace {
436461
}
437462
}
438463

439-
async function warnOnMissingCargoToml() {
440-
const files = await workspace.findFiles('Cargo.toml');
441-
442-
if (files.length < 1) {
443-
window.showWarningMessage(
444-
'A Cargo.toml file must be at the root of the workspace in order to support all features'
445-
);
446-
}
447-
}
448464

449465

450466
function configureLanguage(context: ExtensionContext) {

0 commit comments

Comments
 (0)