Skip to content

Commit 6f48227

Browse files
authored
Merge pull request #235 from bubba/standalone-files
Allow the server to be launched on standalone .hs files
2 parents 49646f1 + 35188d8 commit 6f48227

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

src/extension.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,8 @@ async function activateHie(context: ExtensionContext, document: TextDocument) {
5858

5959
const uri = document.uri;
6060
const folder = workspace.getWorkspaceFolder(uri);
61-
// Don't handle files outside of a folder.
62-
if (!folder) {
63-
return;
64-
}
65-
// If the client already has an LSP server, then don't start a new one.
66-
if (clients.has(folder.uri.toString())) {
61+
// If the client already has an LSP server for this folder, then don't start a new one.
62+
if (folder && clients.has(folder.uri.toString())) {
6763
return;
6864
}
6965

@@ -98,18 +94,18 @@ async function activateHie(context: ExtensionContext, document: TextDocument) {
9894
const forceStart: string = 'Force Start';
9995
window.showErrorMessage(notInstalledMsg, forceStart).then(option => {
10096
if (option === forceStart) {
101-
activateHieNoCheck(context, folder, uri);
97+
activateHieNoCheck(context, uri, folder);
10298
}
10399
});
104100
} else {
105-
activateHieNoCheck(context, folder, uri);
101+
activateHieNoCheck(context, uri, folder);
106102
}
107103
} catch (e) {
108104
console.error(e);
109105
}
110106
}
111107

112-
function activateHieNoCheck(context: ExtensionContext, folder: WorkspaceFolder, uri: Uri) {
108+
function activateHieNoCheck(context: ExtensionContext, uri: Uri, folder?: WorkspaceFolder) {
113109
// Stop right here, if HIE is disabled in the resource/workspace folder.
114110
const enableHIE = workspace.getConfiguration('languageServerHaskell', uri).enableHIE;
115111
if (!enableHIE) {
@@ -131,11 +127,14 @@ function activateHieNoCheck(context: ExtensionContext, folder: WorkspaceFolder,
131127
// Substitute path variables with their corresponding locations.
132128
if (hieExecutablePath !== '') {
133129
hieExecutablePath = hieExecutablePath
134-
.replace('${workspaceFolder}', folder.uri.path)
135-
.replace('${workspaceRoot}', folder.uri.path)
136130
.replace('${HOME}', os.homedir)
137131
.replace('${home}', os.homedir)
138132
.replace(/^~/, os.homedir);
133+
if (folder) {
134+
hieExecutablePath = hieExecutablePath
135+
.replace('${workspaceFolder}', folder.uri.path)
136+
.replace('${workspaceRoot}', folder.uri.path);
137+
}
139138
}
140139

141140
// Set the executable, based on the settings.
@@ -181,16 +180,17 @@ function activateHieNoCheck(context: ExtensionContext, folder: WorkspaceFolder,
181180
};
182181

183182
// Set a unique name per workspace folder (useful for multi-root workspaces).
184-
const langName = 'Haskell HIE (' + folder.name + ')';
183+
const langName = 'Haskell' + (folder ? ` ( ${folder.name} )` : '');
185184
const outputChannel: OutputChannel = window.createOutputChannel(langName);
186185
outputChannel.appendLine('[client] run command = "' + serverPath + ' ' + runArgs.join(' ') + '"');
187186
outputChannel.appendLine('[client] debug command = "' + serverPath + ' ' + debugArgs.join(' ') + '"');
187+
const pat = folder ? `${folder.uri.fsPath}/**/*` : '**/*';
188188
const clientOptions: LanguageClientOptions = {
189189
// Use the document selector to only notify the LSP on files inside the folder
190190
// path for the specific workspace.
191191
documentSelector: [
192-
{ scheme: 'file', language: 'haskell', pattern: `${folder.uri.fsPath}/**/*` },
193-
{ scheme: 'file', language: 'literate haskell', pattern: `${folder.uri.fsPath}/**/*` }
192+
{ scheme: 'file', language: 'haskell', pattern: pat },
193+
{ scheme: 'file', language: 'literate haskell', pattern: pat }
194194
],
195195
synchronize: {
196196
// Synchronize the setting section 'languageServerHaskell' to the server.
@@ -238,13 +238,15 @@ function activateHieNoCheck(context: ExtensionContext, folder: WorkspaceFolder,
238238

239239
// If the client already has an LSP server, then don't start a new one.
240240
// We check this again, as there may be multiple parallel requests.
241-
if (clients.has(folder.uri.toString())) {
241+
if (folder && clients.has(folder.uri.toString())) {
242242
return;
243243
}
244244

245245
// Finally start the client and add it to the list of clients.
246246
langClient.start();
247-
clients.set(folder.uri.toString(), langClient);
247+
if (folder) {
248+
clients.set(folder.uri.toString(), langClient);
249+
}
248250
}
249251

250252
/*

0 commit comments

Comments
 (0)