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

Support workspace folders in OmniSharp launcher #1806

Merged
merged 1 commit into from
Oct 25, 2017
Merged
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@
}
],
"engines": {
"vscode": "^1.15.0"
"vscode": "^1.17.0"
},
"activationEvents": [
"onLanguage:csharp",
Expand Down
187 changes: 105 additions & 82 deletions src/omnisharp/launcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,24 @@ export interface LaunchTarget {
* is included if there are any `*.csproj` files present, but a `*.sln* file is not found.
*/
export function findLaunchTargets(): Thenable<LaunchTarget[]> {
if (!vscode.workspace.rootPath) {
if (!vscode.workspace.workspaceFolders) {
return Promise.resolve([]);
}

const options = Options.Read();

return vscode.workspace.findFiles(
/*include*/ '{**/*.sln,**/*.csproj,**/project.json,**/*.csx,**/*.cake}',
/*exclude*/ '{**/node_modules/**,**/.git/**,**/bower_components/**}',
/*maxResults*/ options.maxProjectResults)
.then(resources => {
return select(resources, vscode.workspace.rootPath);
});
/*include*/ '{**/*.sln,**/*.csproj,**/project.json,**/*.csx,**/*.cake}',
/*exclude*/ '{**/node_modules/**,**/.git/**,**/bower_components/**}',
/*maxResults*/ options.maxProjectResults)
.then(resourcesToLaunchTargets);
}

function select(resources: vscode.Uri[], rootPath: string): LaunchTarget[] {
function resourcesToLaunchTargets(resources: vscode.Uri[]): LaunchTarget[] {
// The list of launch targets is calculated like so:
// * If there are .csproj files, .sln files are considered as launch targets.
// * Any project.json file is considered a launch target.
// * If there is no project.json file in the root, the root as added as a launch target.
// * If there is no project.json file in a workspace folder, the workspace folder as added as a launch target.
// * Additionally, if there are .csproj files, but no .sln file, the root is added as a launch target.
//
// TODO:
Expand All @@ -70,91 +68,116 @@ function select(resources: vscode.Uri[], rootPath: string): LaunchTarget[] {
return [];
}

let targets: LaunchTarget[] = [],
hasCsProjFiles = false,
hasSlnFile = false,
hasProjectJson = false,
hasProjectJsonAtRoot = false,
hasCSX = false,
hasCake = false;
let workspaceFolderToUriMap = new Map<number, vscode.Uri[]>();

hasCsProjFiles = resources.some(isCSharpProject);
for (let resource of resources) {
let folder = vscode.workspace.getWorkspaceFolder(resource);
if (folder) {
let buckets: vscode.Uri[];

resources.forEach(resource => {
// Add .sln files if there are .csproj files
if (hasCsProjFiles && isSolution(resource)) {
hasSlnFile = true;
if (workspaceFolderToUriMap.has(folder.index)) {
buckets = workspaceFolderToUriMap.get(folder.index);
} else {
buckets = [];
workspaceFolderToUriMap.set(folder.index, buckets);
}

targets.push({
label: path.basename(resource.fsPath),
description: vscode.workspace.asRelativePath(path.dirname(resource.fsPath)),
target: resource.fsPath,
directory: path.dirname(resource.fsPath),
kind: LaunchTargetKind.Solution
});
buckets.push(resource);
}
}

// Add project.json files
if (isProjectJson(resource)) {
const dirname = path.dirname(resource.fsPath);
hasProjectJson = true;
hasProjectJsonAtRoot = hasProjectJsonAtRoot || dirname === rootPath;

let targets: LaunchTarget[] = [];

workspaceFolderToUriMap.forEach((resources, folderIndex) =>
{
let hasCsProjFiles = false,
hasSlnFile = false,
hasProjectJson = false,
hasProjectJsonAtRoot = false,
hasCSX = false,
hasCake = false;

hasCsProjFiles = resources.some(isCSharpProject);

let folder = vscode.workspace.workspaceFolders[folderIndex];
let folderPath = folder.uri.fsPath;

resources.forEach(resource => {
// Add .sln files if there are .csproj files
if (hasCsProjFiles && isSolution(resource)) {
hasSlnFile = true;

targets.push({
label: path.basename(resource.fsPath),
description: vscode.workspace.asRelativePath(path.dirname(resource.fsPath)),
target: resource.fsPath,
directory: path.dirname(resource.fsPath),
kind: LaunchTargetKind.Solution
});
}

// Add project.json files
if (isProjectJson(resource)) {
const dirname = path.dirname(resource.fsPath);
hasProjectJson = true;
hasProjectJsonAtRoot = hasProjectJsonAtRoot || dirname === folderPath;

targets.push({
label: path.basename(resource.fsPath),
description: vscode.workspace.asRelativePath(path.dirname(resource.fsPath)),
target: dirname,
directory: dirname,
kind: LaunchTargetKind.ProjectJson
});
}

// Discover if there is any CSX file
if (!hasCSX && isCsx(resource)) {
hasCSX = true;
}

// Discover if there is any Cake file
if (!hasCake && isCake(resource)) {
hasCake = true;
}
});

// Add the root folder under the following circumstances:
// * If there are .csproj files, but no .sln file, and none in the root.
// * If there are project.json files, but none in the root.
if ((hasCsProjFiles && !hasSlnFile) || (hasProjectJson && !hasProjectJsonAtRoot)) {
targets.push({
label: path.basename(resource.fsPath),
description: vscode.workspace.asRelativePath(path.dirname(resource.fsPath)),
target: dirname,
directory: dirname,
kind: LaunchTargetKind.ProjectJson
label: path.basename(folderPath),
description: '',
target: folderPath,
directory: folderPath,
kind: LaunchTargetKind.Folder
});
}

// Discover if there is any CSX file
if (!hasCSX && isCsx(resource)) {
hasCSX = true;

// if we noticed any CSX file(s), add a single CSX-specific target pointing at the root folder
if (hasCSX) {
targets.push({
label: "CSX",
description: path.basename(folderPath),
target: folderPath,
directory: folderPath,
kind: LaunchTargetKind.Csx
});
}

// Discover if there is any Cake file
if (!hasCake && isCake(resource)) {
hasCake = true;

// if we noticed any Cake file(s), add a single Cake-specific target pointing at the root folder
if (hasCake) {
targets.push({
label: "Cake",
description: path.basename(folderPath),
target: folderPath,
directory: folderPath,
kind: LaunchTargetKind.Cake
});
}
});

// Add the root folder under the following circumstances:
// * If there are .csproj files, but no .sln file, and none in the root.
// * If there are project.json files, but none in the root.
if ((hasCsProjFiles && !hasSlnFile) || (hasProjectJson && !hasProjectJsonAtRoot)) {
targets.push({
label: path.basename(rootPath),
description: '',
target: rootPath,
directory: rootPath,
kind: LaunchTargetKind.Folder
});
}

// if we noticed any CSX file(s), add a single CSX-specific target pointing at the root folder
if (hasCSX) {
targets.push({
label: "CSX",
description: path.basename(rootPath),
target: rootPath,
directory: rootPath,
kind: LaunchTargetKind.Csx
});
}

// if we noticed any Cake file(s), add a single Cake-specific target pointing at the root folder
if (hasCake) {
targets.push({
label: "Cake",
description: path.basename(rootPath),
target: rootPath,
directory: rootPath,
kind: LaunchTargetKind.Cake
});
}

return targets.sort((a, b) => a.directory.localeCompare(b.directory));
}

Expand Down