From 536e00c2df000c9bf3291a59c86ee03b63de4dce Mon Sep 17 00:00:00 2001 From: Vito De Tullio Date: Wed, 17 Aug 2022 18:09:32 +0200 Subject: [PATCH 1/3] add "--lsp" flag to bsc to start an LSP server --- src/BsConfig.ts | 6 ++++++ src/ProgramBuilder.ts | 24 +++++++++++++++--------- src/cli.ts | 8 ++++++++ 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/BsConfig.ts b/src/BsConfig.ts index a0333bf11..3e7026a0b 100644 --- a/src/BsConfig.ts +++ b/src/BsConfig.ts @@ -158,4 +158,10 @@ export interface BsConfig { * @default false */ allowBrighterScriptInBrightScript?: boolean; + + /** + * Start a standalone LSP server + * @default false + */ + lsp?: boolean; } diff --git a/src/ProgramBuilder.ts b/src/ProgramBuilder.ts index cd81aef31..066d17702 100644 --- a/src/ProgramBuilder.ts +++ b/src/ProgramBuilder.ts @@ -12,6 +12,7 @@ import PluginInterface from './PluginInterface'; import * as diagnosticUtils from './diagnosticUtils'; import * as fsExtra from 'fs-extra'; import * as requireRelative from 'require-relative'; +import { LanguageServer } from './LanguageServer'; /** * A runner class that handles @@ -120,17 +121,22 @@ export class ProgramBuilder { } this.logger.logLevel = this.options.logLevel as LogLevel; - this.program = this.createProgram(); + if (this.options.lsp) { + const server = new LanguageServer(); + server.run(); + } else { + this.program = this.createProgram(); - //parse every file in the entire project - await this.loadAllFilesAST(); + //parse every file in the entire project + await this.loadAllFilesAST(); - if (this.options.watch) { - this.logger.log('Starting compilation in watch mode...'); - await this.runOnce(); - this.enableWatchMode(); - } else { - await this.runOnce(); + if (this.options.watch) { + this.logger.log('Starting compilation in watch mode...'); + await this.runOnce(); + this.enableWatchMode(); + } else { + await this.runOnce(); + } } } diff --git a/src/cli.ts b/src/cli.ts index a8356c805..0ecb9fd2b 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -31,6 +31,7 @@ let options = yargs .option('source-root', { type: 'string', description: 'Override the root directory path where debugger should locate the source files. The location will be embedded in the source map to help debuggers locate the original source files. This only applies to files found within rootDir. This is useful when you want to preprocess files before passing them to BrighterScript, and want a debugger to open the original files.' }) .option('watch', { type: 'boolean', defaultDescription: 'false', description: 'Watch input files.' }) .option('require', { type: 'array', description: 'A list of modules to require() on startup. Useful for doing things like ts-node registration.' }) + .option('lsp', { type: 'boolean', defaultDescription: 'false', description: 'Start a brightscript language server' }) .check(argv => { const diagnosticLevel = argv.diagnosticLevel as string; //if we have the diagnostic level and it's not a known value, then fail @@ -44,6 +45,13 @@ let options = yargs util.resolvePathsRelativeTo(argv, 'require', cwd); return true; }) + .middleware(argv => { + // as lsp relies on stdio to communicate, logs may interfere + if (argv.lsp) { + argv.logLevel = 'off'; + argv.diagnosticLevel = 'off'; + } + }) .argv; let builder = new ProgramBuilder(); From d432588ddb16c4d5a80c8fcb29680b53ead4120e Mon Sep 17 00:00:00 2001 From: Vito De Tullio Date: Wed, 17 Aug 2022 18:51:18 +0200 Subject: [PATCH 2/3] cleanups, move implementation in cli.ts --- src/BsConfig.ts | 6 ------ src/ProgramBuilder.ts | 24 +++++++++--------------- src/cli.ts | 34 ++++++++++++++++------------------ 3 files changed, 25 insertions(+), 39 deletions(-) diff --git a/src/BsConfig.ts b/src/BsConfig.ts index 3e7026a0b..a0333bf11 100644 --- a/src/BsConfig.ts +++ b/src/BsConfig.ts @@ -158,10 +158,4 @@ export interface BsConfig { * @default false */ allowBrighterScriptInBrightScript?: boolean; - - /** - * Start a standalone LSP server - * @default false - */ - lsp?: boolean; } diff --git a/src/ProgramBuilder.ts b/src/ProgramBuilder.ts index 066d17702..cd81aef31 100644 --- a/src/ProgramBuilder.ts +++ b/src/ProgramBuilder.ts @@ -12,7 +12,6 @@ import PluginInterface from './PluginInterface'; import * as diagnosticUtils from './diagnosticUtils'; import * as fsExtra from 'fs-extra'; import * as requireRelative from 'require-relative'; -import { LanguageServer } from './LanguageServer'; /** * A runner class that handles @@ -121,22 +120,17 @@ export class ProgramBuilder { } this.logger.logLevel = this.options.logLevel as LogLevel; - if (this.options.lsp) { - const server = new LanguageServer(); - server.run(); - } else { - this.program = this.createProgram(); + this.program = this.createProgram(); - //parse every file in the entire project - await this.loadAllFilesAST(); + //parse every file in the entire project + await this.loadAllFilesAST(); - if (this.options.watch) { - this.logger.log('Starting compilation in watch mode...'); - await this.runOnce(); - this.enableWatchMode(); - } else { - await this.runOnce(); - } + if (this.options.watch) { + this.logger.log('Starting compilation in watch mode...'); + await this.runOnce(); + this.enableWatchMode(); + } else { + await this.runOnce(); } } diff --git a/src/cli.ts b/src/cli.ts index 0ecb9fd2b..39eec24c8 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -4,6 +4,7 @@ import * as path from 'path'; import { ProgramBuilder } from './ProgramBuilder'; import { DiagnosticSeverity } from 'vscode-languageserver'; import util from './util'; +import { LanguageServer } from './LanguageServer'; let options = yargs .usage('$0', 'BrighterScript, a superset of Roku\'s BrightScript language') @@ -45,24 +46,21 @@ let options = yargs util.resolvePathsRelativeTo(argv, 'require', cwd); return true; }) - .middleware(argv => { - // as lsp relies on stdio to communicate, logs may interfere - if (argv.lsp) { - argv.logLevel = 'off'; - argv.diagnosticLevel = 'off'; - } - }) .argv; -let builder = new ProgramBuilder(); -builder.run(options).then(() => { - //if this is a single run (i.e. not watch mode) and there are error diagnostics, return an error code - const hasError = !!builder.getDiagnostics().find(x => x.severity === DiagnosticSeverity.Error); - if (builder.options.watch === false && hasError) { +if (options.lsp) { + const server = new LanguageServer(); + server.run(); +} else { + let builder = new ProgramBuilder(); + builder.run(options).then(() => { + //if this is a single run (i.e. not watch mode) and there are error diagnostics, return an error code + const hasError = !!builder.getDiagnostics().find(x => x.severity === DiagnosticSeverity.Error); + if (builder.options.watch === false && hasError) { + process.exit(1); + } + }).catch((error) => { + console.error(error); process.exit(1); - } -}).catch((error) => { - console.error(error); - process.exit(1); -}); - + }); +} From 6cbdee4415949f7f32c6a47448c51aceb09c8a51 Mon Sep 17 00:00:00 2001 From: Vito De Tullio Date: Wed, 17 Aug 2022 18:52:47 +0200 Subject: [PATCH 3/3] better description --- src/cli.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cli.ts b/src/cli.ts index 39eec24c8..6d71352aa 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -32,7 +32,7 @@ let options = yargs .option('source-root', { type: 'string', description: 'Override the root directory path where debugger should locate the source files. The location will be embedded in the source map to help debuggers locate the original source files. This only applies to files found within rootDir. This is useful when you want to preprocess files before passing them to BrighterScript, and want a debugger to open the original files.' }) .option('watch', { type: 'boolean', defaultDescription: 'false', description: 'Watch input files.' }) .option('require', { type: 'array', description: 'A list of modules to require() on startup. Useful for doing things like ts-node registration.' }) - .option('lsp', { type: 'boolean', defaultDescription: 'false', description: 'Start a brightscript language server' }) + .option('lsp', { type: 'boolean', defaultDescription: 'false', description: 'Run brighterscript as a language server.' }) .check(argv => { const diagnosticLevel = argv.diagnosticLevel as string; //if we have the diagnostic level and it's not a known value, then fail