Skip to content

Commit

Permalink
Merge pull request #184 from tncrazvan/master
Browse files Browse the repository at this point in the history
Feature: restart language server on project switching when using workspaces
  • Loading branch information
tm1000 authored Jul 25, 2022
2 parents 8a7bec4 + 6b17bf5 commit e812c41
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 21 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions src/LanguageServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,22 @@ export class LanguageServer {
this.languageClient.onTelemetry(this.onTelemetry.bind(this));
}

/**
* This will NOT restart the server.
* @param workspacePath
*/
public setWorkspacePath(workspacePath: string): void {
this.workspacePath = workspacePath;
}

/**
* This will NOT restart the server.
* @param psalmConfigPath
*/
public setPsalmConfigPath(psalmConfigPath: string): void {
this.psalmConfigPath = psalmConfigPath;
}

public createDefaultErrorHandler(maxRestartCount?: number): ErrorHandler {
if (maxRestartCount !== undefined && maxRestartCount < 0) {
throw new Error(`Invalid maxRestartCount: ${maxRestartCount}`);
Expand Down
2 changes: 1 addition & 1 deletion src/LanguageServerErrorHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default class LanguageServerErrorHandler implements ErrorHandler {
if (this.restarts.length <= this.maxRestartCount) {
return CloseAction.Restart;
} else {
let diff =
const diff =
this.restarts[this.restarts.length - 1] - this.restarts[0];
if (diff <= 3 * 60 * 1000) {
void showReportIssueErrorMessage(
Expand Down
7 changes: 5 additions & 2 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ function reportIssue(
'report_issue_template.md'
);

const userSettings = Object.entries(configurationService.getAll())
const userSettings = Object.entries(
configurationService.getAll()
)
.map(([key, value]) => `${key}: ${JSON.stringify(value)}`)
.join(EOL);
const psalmLogs = loggingService.getContent().join(EOL);
Expand All @@ -88,7 +90,8 @@ function reportIssue(
let psalmVersion: string | null = 'unknown';
try {
psalmVersion =
(await client.getPsalmLanguageServerVersion()) ?? 'unknown';
(await client.getPsalmLanguageServerVersion()) ??
'unknown';
} catch (err) {
psalmVersion = err.message;
}
Expand Down
103 changes: 85 additions & 18 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export async function activate(
const configurationService = new ConfigurationService();
await configurationService.init();

//Set Logging Level
// Set Logging Level
loggingService.setOutputLevel(
configurationService.get<LogLevel>('logLevel')
);
Expand All @@ -37,9 +37,60 @@ export async function activate(
return;
}

const workspacePath = workspaceFolders[0].uri.fsPath;
const getCurrentWorkspace = (
workspaceFolders: readonly vscode.WorkspaceFolder[]
) => {
const activeWorkspace = vscode.window.activeTextEditor
? vscode.workspace.getWorkspaceFolder(
vscode.window.activeTextEditor.document.uri
)
: workspaceFolders[0];

const configPaths = configurationService.get<string[]>('configPaths') || [];
const workspacePath = activeWorkspace
? activeWorkspace.uri.fsPath
: workspaceFolders[0].uri.fsPath;

return { workspacePath };
};

const getOptions = async () => {
const configPaths =
configurationService.get<string[]>('configPaths') || [];

const psalmXMLFiles = await vscode.workspace.findFiles(
`{${configPaths.join(',')}}`
// `**/vendor/**/{${configPaths.join(',')}}`
);

const psalmXMLPaths = psalmXMLFiles.map((uri) => {
if (process.platform === 'win32') {
return uri.path.replace(/\//g, '\\').replace(/^\\/g, '');
}
return uri.path;
});

const { workspacePath } = getCurrentWorkspace(workspaceFolders);

const configXml =
psalmXMLPaths.find((path) => path.startsWith(workspacePath)) ??
psalmXMLPaths[0];

return {
configPaths,
psalmXMLFiles,
psalmXMLPaths,
configXml,
workspacePath,
};
};

let {
configPaths,
// psalmXMLFiles,
psalmXMLPaths,
configXml,
workspacePath,
} = await getOptions();

if (!configPaths.length) {
loggingService.logError(
Expand All @@ -48,18 +99,6 @@ export async function activate(
return;
}

const psalmXMLFiles = await vscode.workspace.findFiles(
`{${configPaths.join(',')}}`
// `**/vendor/**/{${configPaths.join(',')}}`
);

const psalmXMLPaths = psalmXMLFiles.map((uri) => {
if (process.platform === 'win32') {
return uri.path.replace(/\//g, '\\').replace(/^\\/g, '');
}
return uri.path;
});

if (!psalmXMLPaths.length) {
// no psalm.xml found
loggingService.logError(
Expand All @@ -73,11 +112,9 @@ export async function activate(
psalmXMLPaths
);

const configXml = psalmXMLPaths[0];

loggingService.logDebug(`Selecting first found config file: ${configXml}`);

const configWatcher = vscode.workspace.createFileSystemWatcher(configXml);
let configWatcher = vscode.workspace.createFileSystemWatcher(configXml);

const languageServer = new LanguageServer(
context,
Expand All @@ -88,6 +125,17 @@ export async function activate(
loggingService
);

// restart the language server when changing workspaces
const onWorkspacePathChange = async () => {
//kill the previous watcher
configWatcher.dispose();
configWatcher = vscode.workspace.createFileSystemWatcher(configXml);
loggingService.logInfo(`Workspace changed: ${workspacePath}`);
languageServer.setWorkspacePath(workspacePath);
languageServer.setPsalmConfigPath(configXml);
languageServer.restart();
};

const onConfigChange = () => {
loggingService.logInfo(`Config file changed: ${configXml}`);
languageServer.restart();
Expand Down Expand Up @@ -129,5 +177,24 @@ export async function activate(
await configurationService.init();
});

vscode.window.onDidChangeActiveTextEditor(async (e) => {
if (!e) {
return;
}

const options = await getOptions();

if (!options.workspacePath || workspacePath === options.workspacePath)
return;

configPaths = options.configPaths;
configXml = options.configXml;
// psalmXMLFiles = options.psalmXMLFiles;
psalmXMLPaths = options.psalmXMLPaths;
workspacePath = options.workspacePath;

onWorkspacePathChange();
});

loggingService.logDebug('Finished Extension Activation');
}

0 comments on commit e812c41

Please sign in to comment.