Skip to content

Commit

Permalink
feat: Support workspace/didChangeConfiguration event (lifeart#254)
Browse files Browse the repository at this point in the history
* remove use of `any` type for initializers

* log to root of ELS package

This creates a debugging log file at the root of the ELS package, rather than inside the `lib` directory.

The previous location was extremely difficult to find, since it’s alongside the compiled TypeScript output.

An environment variable has also been added to more easily turn on the file logger.

* perform logging to remote console if possible

* respond to the `workspace/didChangeConfiguration` event

Co-authored-by: Alex Kanunnikov <lifeart92@gmail.com>
  • Loading branch information
alexlafroscia and lifeart authored Apr 18, 2021
1 parent b119586 commit 05d97cb
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 21 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ dist/
coverage
inst/
.nyc_output

# Debug Logging
debug.log
37 changes: 26 additions & 11 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ import { URI } from 'vscode-uri';
import { MatchResultType } from './utils/path-matcher';
import { FileChangeType } from 'vscode-languageserver/node';
import { debounce } from 'lodash';
import { Config, Initializer } from './types';

export default class Server {
initializers: any[] = [];
initializers: Initializer[] = [];
lazyInit = false;
// Create a connection for the server. The connection defaults to Node's IPC as a transport, but
// also supports stdio via command line flag
Expand Down Expand Up @@ -117,6 +118,22 @@ export default class Server {
}
}

setConfiguration(config: Config) {
if (config.addons) {
this.projectRoots.setLocalAddons(config.addons);
}

if (config.ignoredProjects) {
this.projectRoots.setIgnoredProjects(config.ignoredProjects);
}

if (config.useBuiltinLinting === false) {
this.templateLinter.disable();
} else if (config.useBuiltinLinting === true) {
this.templateLinter.enable();
}
}

documentSymbolProviders: DocumentSymbolProvider[] = [new JSDocumentSymbolProvider(), new HBSDocumentSymbolProvider()];

templateCompletionProvider: TemplateCompletionProvider = new TemplateCompletionProvider(this);
Expand All @@ -129,23 +146,16 @@ export default class Server {
referenceProvider: ReferenceProvider = new ReferenceProvider(this);
codeActionProvider: CodeActionProvider = new CodeActionProvider(this);
executeInitializers() {
this.initializers.forEach((cb: any) => cb());
this.initializers.forEach((cb) => cb());
this.initializers = [];
}
private onInitialized() {
if (this.connection.workspace && this.clientCapabilities && this.clientCapabilities.workspace && this.clientCapabilities.workspace.workspaceFolders) {
this.connection.workspace.onDidChangeWorkspaceFolders(this.onDidChangeWorkspaceFolders.bind(this));
}

this.executors['els.setConfig'] = async (_, __, [config]: [{ local: { addons: string[]; ignoredProjects: string[]; useBuiltinLinting: boolean } }]) => {
this.projectRoots.setLocalAddons(config.local.addons);
this.projectRoots.setIgnoredProjects(config.local.ignoredProjects);

if (config.local.useBuiltinLinting === false) {
this.templateLinter.disable();
} else if (config.local.useBuiltinLinting === true) {
this.templateLinter.enable();
}
this.executors['els.setConfig'] = async (_, __, [config]: [{ local: Config }]) => {
this.setConfiguration(config.local);

if (this.lazyInit) {
this.executeInitializers();
Expand Down Expand Up @@ -280,6 +290,7 @@ export default class Server {
this.documents.onDidChangeContent(this.onDidChangeContent);
this.documents.onDidOpen(this.onDidChangeContent);
this.connection.onDidChangeWatchedFiles(this.onDidChangeWatchedFiles.bind(this));
this.connection.onDidChangeConfiguration(this.onDidChangeConfiguration.bind(this));
this.connection.onDocumentSymbol(this.onDocumentSymbol.bind(this));
this.connection.onDefinition(this.definitionProvider.handler);
this.connection.onCompletion(this.onCompletion.bind(this));
Expand Down Expand Up @@ -529,6 +540,10 @@ export default class Server {
// const Deleted = 3;
}

private onDidChangeConfiguration({ settings }: { settings: Config }) {
this.setConfiguration(settings);
}

private async onReference(params: ReferenceParams): Promise<Location[]> {
return await this.referenceProvider.provideReferences(params);
}
Expand Down
7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export type Initializer = () => void;

export interface Config {
addons: string[];
ignoredProjects: string[];
useBuiltinLinting: boolean;
}
23 changes: 13 additions & 10 deletions src/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import * as fs from 'fs';
import * as util from 'util';
import { resolve } from 'path';

import { RemoteConsole } from 'vscode-languageserver/node';

const debug = false;
const log_file = debug ? fs.createWriteStream(__dirname + '/debug.log', { flags: 'w' }) : null;
// Log debugging to the ELS package root, if possible
const debug = process.env.ELS_DEBUG || false;
const log_file = debug ? fs.createWriteStream(resolve(__dirname, '../../debug.log'), { flags: 'w' }) : null;

let remoteConsole: RemoteConsole | null = null;

export function logError(err: any) {
Expand Down Expand Up @@ -43,16 +46,16 @@ export function safeStringify(obj: unknown, indent = 2) {
return retVal;
}

export function log(...args: any[]) {
if (!debug || !log_file) {
return;
export function log(...args: unknown[]) {
const output = args.map((a) => safeStringify(a)).join(' ');

if (remoteConsole) {
remoteConsole.log(output);
}

const output = args
.map((a: any) => {
return safeStringify(a);
})
.join(' ');
if (!log_file) {
return;
}

log_file.write('----------------------------------------' + '\r\n');
log_file.write(util.format(output) + '\r\n');
Expand Down

0 comments on commit 05d97cb

Please # to comment.