|
4 | 4 | * This source code is licensed under the MIT license found in the
|
5 | 5 | * LICENSE file in the root directory of this source tree.
|
6 | 6 | */
|
| 7 | + |
| 8 | +import {TextDocument} from 'vscode-languageserver-textdocument'; |
| 9 | +import { |
| 10 | + createConnection, |
| 11 | + type InitializeParams, |
| 12 | + type InitializeResult, |
| 13 | + ProposedFeatures, |
| 14 | + TextDocuments, |
| 15 | + TextDocumentSyncKind, |
| 16 | +} from 'vscode-languageserver/node'; |
| 17 | +import {compile} from './compiler'; |
| 18 | +import {type PluginOptions} from 'babel-plugin-react-compiler/src'; |
| 19 | +import {resolveReactConfig} from './compiler/options'; |
| 20 | +import {type BabelFileResult} from '@babel/core'; |
| 21 | + |
| 22 | +const SUPPORTED_LANGUAGE_IDS = new Set([ |
| 23 | + 'javascript', |
| 24 | + 'javascriptreact', |
| 25 | + 'typescript', |
| 26 | + 'typescriptreact', |
| 27 | +]); |
| 28 | + |
| 29 | +const connection = createConnection(ProposedFeatures.all); |
| 30 | +const documents = new TextDocuments(TextDocument); |
| 31 | + |
| 32 | +let compilerOptions: PluginOptions | null = null; |
| 33 | +let lastResult: BabelFileResult | null = null; |
| 34 | + |
| 35 | +connection.onInitialize((_params: InitializeParams) => { |
| 36 | + // TODO(@poteto) get config fr |
| 37 | + compilerOptions = resolveReactConfig('.'); |
| 38 | + const result: InitializeResult = { |
| 39 | + capabilities: { |
| 40 | + textDocumentSync: TextDocumentSyncKind.Full, |
| 41 | + codeLensProvider: {resolveProvider: true}, |
| 42 | + }, |
| 43 | + }; |
| 44 | + return result; |
| 45 | +}); |
| 46 | + |
| 47 | +connection.onInitialized(() => { |
| 48 | + connection.console.log('initialized'); |
| 49 | +}); |
| 50 | + |
| 51 | +documents.onDidOpen(async event => { |
| 52 | + if (SUPPORTED_LANGUAGE_IDS.has(event.document.languageId)) { |
| 53 | + const text = event.document.getText(); |
| 54 | + const result = await compile({ |
| 55 | + text, |
| 56 | + file: event.document.uri, |
| 57 | + options: compilerOptions, |
| 58 | + }); |
| 59 | + if (result.code != null) { |
| 60 | + lastResult = result; |
| 61 | + } |
| 62 | + } |
| 63 | +}); |
| 64 | + |
| 65 | +documents.onDidChangeContent(async event => { |
| 66 | + if (SUPPORTED_LANGUAGE_IDS.has(event.document.languageId)) { |
| 67 | + const text = event.document.getText(); |
| 68 | + const result = await compile({ |
| 69 | + text, |
| 70 | + file: event.document.uri, |
| 71 | + options: compilerOptions, |
| 72 | + }); |
| 73 | + if (result.code != null) { |
| 74 | + lastResult = result; |
| 75 | + } |
| 76 | + } |
| 77 | +}); |
| 78 | + |
| 79 | +connection.onDidChangeWatchedFiles(change => { |
| 80 | + connection.console.log( |
| 81 | + change.changes.map(c => `File changed: ${c.uri}`).join('\n'), |
| 82 | + ); |
| 83 | +}); |
| 84 | + |
| 85 | +connection.onCodeLens(params => { |
| 86 | + connection.console.log('lastResult: ' + JSON.stringify(lastResult, null, 2)); |
| 87 | + connection.console.log('params: ' + JSON.stringify(params, null, 2)); |
| 88 | + return []; |
| 89 | +}); |
| 90 | + |
| 91 | +documents.listen(connection); |
| 92 | +connection.listen(); |
| 93 | +connection.console.info(`React Analyzer running in node ${process.version}`); |
0 commit comments