-
Notifications
You must be signed in to change notification settings - Fork 44
/
utilities.ts
67 lines (60 loc) · 2.02 KB
/
utilities.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import * as fs from 'fs-extra';
import * as path from 'path';
import { LogLevel, Uri, WorkspaceFolder } from 'vscode';
import { Trace } from 'vscode-jsonrpc/node';
import { getWorkspaceFolders } from './vscodeapi';
function logLevelToTrace(logLevel: LogLevel): Trace {
switch (logLevel) {
case LogLevel.Error:
case LogLevel.Warning:
case LogLevel.Info:
return Trace.Messages;
case LogLevel.Debug:
case LogLevel.Trace:
return Trace.Verbose;
case LogLevel.Off:
default:
return Trace.Off;
}
}
export function getLSClientTraceLevel(channelLogLevel: LogLevel, globalLogLevel: LogLevel): Trace {
if (channelLogLevel === LogLevel.Off) {
return logLevelToTrace(globalLogLevel);
}
if (globalLogLevel === LogLevel.Off) {
return logLevelToTrace(channelLogLevel);
}
const level = logLevelToTrace(channelLogLevel <= globalLogLevel ? channelLogLevel : globalLogLevel);
return level;
}
export async function getProjectRoot(): Promise<WorkspaceFolder> {
const workspaces: readonly WorkspaceFolder[] = getWorkspaceFolders();
if (workspaces.length === 0) {
return {
uri: Uri.file(process.cwd()),
name: path.basename(process.cwd()),
index: 0,
};
} else if (workspaces.length === 1) {
return workspaces[0];
} else {
let rootWorkspace = workspaces[0];
let root = undefined;
for (const w of workspaces) {
if (await fs.pathExists(w.uri.fsPath)) {
root = w.uri.fsPath;
rootWorkspace = w;
break;
}
}
for (const w of workspaces) {
if (root && root.length > w.uri.fsPath.length && (await fs.pathExists(w.uri.fsPath))) {
root = w.uri.fsPath;
rootWorkspace = w;
}
}
return rootWorkspace;
}
}