From ba81b0ea3cd0285efe7bafc23dbc6fafbafb7ef8 Mon Sep 17 00:00:00 2001 From: Martin Duhem Date: Thu, 18 Oct 2018 08:04:08 +0200 Subject: [PATCH 1/3] Remove unused imports --- vscode-dotty/src/extension.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/vscode-dotty/src/extension.ts b/vscode-dotty/src/extension.ts index 4de85155b867..bf3c4062dc54 100644 --- a/vscode-dotty/src/extension.ts +++ b/vscode-dotty/src/extension.ts @@ -13,8 +13,6 @@ import * as vscode from 'vscode'; import { LanguageClient, LanguageClientOptions, RevealOutputChannelOn, ServerOptions } from 'vscode-languageclient'; import { enableOldServerWorkaround } from './compat' -import { WorksheetPublishOutputNotification } from './protocol' -import * as worksheet from './worksheet' import * as features from './features' export let client: LanguageClient From 688c149bca71036a1a933e94f2bc0212e7a2a5b9 Mon Sep 17 00:00:00 2001 From: Martin Duhem Date: Thu, 18 Oct 2018 11:05:29 +0200 Subject: [PATCH 2/3] IDE: Find and add workspace root if there are none When the IDE is opened without a workspace (no folder is open), and then a Scala source is opened, we will now search for the closest possible workspace root (the closest parent directory that contains a `build.sbt`). If no such directory can be found, the parent directory of the file that has been opened is chosen. This folder is then added as a workspace folder. Adding a new workspace folder triggers a reloading of the extension. The code of the extension will be executed in an environment where a workspace root is set, and Dotty IDE will be able to start correctly. Fixes #5281 --- vscode-dotty/src/extension.ts | 73 +++++++++++++++++++++++++---------- 1 file changed, 53 insertions(+), 20 deletions(-) diff --git a/vscode-dotty/src/extension.ts b/vscode-dotty/src/extension.ts index bf3c4062dc54..08d1b346e46f 100644 --- a/vscode-dotty/src/extension.ts +++ b/vscode-dotty/src/extension.ts @@ -66,29 +66,62 @@ export function activate(context: ExtensionContext) { }) } else if (!fs.existsSync(disableDottyIDEFile)) { - let configuredProject: Thenable = Promise.resolve() - if (!isConfiguredProject()) { - configuredProject = vscode.window.showInformationMessage( - "This looks like an unconfigured Scala project. Would you like to start the Dotty IDE?", - "Yes", "No" - ).then(choice => { - if (choice === "Yes") { - bootstrapSbtProject(buildSbtFileSource, dottyPluginSbtFileSource) - return Promise.resolve() - } else if (choice === "No") { - fs.appendFile(disableDottyIDEFile, "", _ => {}) - return Promise.reject() - } - }) - .then(_ => connectToSbt(coursierPath)) - .then(sbt => { - return withProgress("Configuring Dotty IDE...", configureIDE(sbt)) - .then(_ => { sbtserver.tellSbt(outputChannel, sbt, "exit") }) + + if (!vscode.workspace.workspaceFolders) { + if (vscode.window.activeTextEditor) { + setWorkspaceAndReload(vscode.window.activeTextEditor.document) + } + } else { + let configuredProject: Thenable = Promise.resolve() + if (!isConfiguredProject()) { + configuredProject = vscode.window.showInformationMessage( + "This looks like an unconfigured Scala project. Would you like to start the Dotty IDE?", + "Yes", "No" + ).then(choice => { + if (choice === "Yes") { + bootstrapSbtProject(buildSbtFileSource, dottyPluginSbtFileSource) + return Promise.resolve() + } else if (choice === "No") { + fs.appendFile(disableDottyIDEFile, "", _ => {}) + return Promise.reject() + } }) + .then(_ => connectToSbt(coursierPath)) + .then(sbt => { + return withProgress("Configuring Dotty IDE...", configureIDE(sbt)) + .then(_ => { sbtserver.tellSbt(outputChannel, sbt, "exit") }) + }) + } + + configuredProject + .then(_ => runLanguageServer(coursierPath, languageServerArtifactFile)) } + } +} - configuredProject - .then(_ => runLanguageServer(coursierPath, languageServerArtifactFile)) +/** + * Find and set a workspace root if no folders are open in the workspace. If there are already + * folders open in the workspace, do nothing. + * + * Adding a first folder to the workspace completely reloads the extension. + */ +function setWorkspaceAndReload(document: vscode.TextDocument) { + const documentPath = path.parse(document.uri.path).dir + const workspaceRoot = findWorkspaceRoot(documentPath) || documentPath + vscode.workspace.updateWorkspaceFolders(0, null, { uri: vscode.Uri.file(workspaceRoot) }) +} + +/** + * Find the closest parent of `current` that contains a `build.sbt`. + */ +function findWorkspaceRoot(current: string): string | undefined { + const build = path.join(current, "build.sbt") + if (fs.existsSync(build)) return current + else { + const parent = path.resolve(current, "..") + if (parent != current) { + return findWorkspaceRoot(parent) + } } } From 9f441f990866b26b33e948fd01b01796acc99366 Mon Sep 17 00:00:00 2001 From: Martin Duhem Date: Thu, 18 Oct 2018 15:39:40 +0200 Subject: [PATCH 3/3] Fix for Windows --- vscode-dotty/src/extension.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vscode-dotty/src/extension.ts b/vscode-dotty/src/extension.ts index 08d1b346e46f..4c42966243cc 100644 --- a/vscode-dotty/src/extension.ts +++ b/vscode-dotty/src/extension.ts @@ -106,7 +106,7 @@ export function activate(context: ExtensionContext) { * Adding a first folder to the workspace completely reloads the extension. */ function setWorkspaceAndReload(document: vscode.TextDocument) { - const documentPath = path.parse(document.uri.path).dir + const documentPath = path.parse(document.uri.fsPath).dir const workspaceRoot = findWorkspaceRoot(documentPath) || documentPath vscode.workspace.updateWorkspaceFolders(0, null, { uri: vscode.Uri.file(workspaceRoot) }) }