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

search for the cargo toml based on the current file #457

Merged
Merged
Changes from 1 commit
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
37 changes: 26 additions & 11 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { activateTaskProvider, runCommand } from './tasks';

import * as child_process from 'child_process';
import * as fs from 'fs';
import path = require('path');

import { commands, ExtensionContext, IndentAction, languages, TextEditor,
TextEditorEdit, window, workspace, TextDocument, WorkspaceFolder, Disposable, Uri,
Expand Down Expand Up @@ -53,6 +54,10 @@ function didOpenTextDocument(document: TextDocument, context: ExtensionContext):
return;
}
folder = getOuterMostWorkspaceFolder(folder);
folder = getCargoTomlWorkspace(folder, document.uri.fsPath);
if (!folder) {
return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can/should we issue a warning about missing Cargo.toml here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes probably a good idea

Copy link
Contributor Author

@jannickj jannickj Dec 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My only concern is that you will get a pop up if you are just opening a .rs file outside it's project

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay I've come up with a solution that will work in both use cases.

}

if (!workspaces.has(folder.uri.toString())) {
const workspace = new ClientWorkspace(folder);
Expand Down Expand Up @@ -82,6 +87,27 @@ function sortedWorkspaceFolders(): string[] {
return _sortedWorkspaceFolders || [];
}

function getCargoTomlWorkspace(cur_workspace: WorkspaceFolder, file_path: string): WorkspaceFolder | undefined {
let current = file_path;
const workspace_root = path.parse(cur_workspace.uri.fsPath).dir;
while (true) {
const old = current;
current = path.dirname(current);
if (old == current) {
break;
}
if (workspace_root == path.parse(current).dir) {
break;
}

const cargo_path = path.join(current, 'Cargo.toml');
if (fs.existsSync(cargo_path)) {
return { ...cur_workspace, uri: Uri.parse(current) };
}
}
return undefined;
}

function getOuterMostWorkspaceFolder(folder: WorkspaceFolder): WorkspaceFolder {
const sorted = sortedWorkspaceFolders();
for (const element of sorted) {
Expand Down Expand Up @@ -146,8 +172,6 @@ class ClientWorkspace {
}

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

startSpinner('RLS', 'Starting');

Expand Down Expand Up @@ -436,15 +460,6 @@ class ClientWorkspace {
}
}

async function warnOnMissingCargoToml() {
const files = await workspace.findFiles('Cargo.toml');

if (files.length < 1) {
window.showWarningMessage(
'A Cargo.toml file must be at the root of the workspace in order to support all features'
);
}
}


function configureLanguage(context: ExtensionContext) {
Expand Down