From 48a196cdad7d0b7137db16341a8ab08438e16c1b Mon Sep 17 00:00:00 2001 From: Ruben Fiszel Date: Mon, 3 Apr 2023 13:25:54 +0200 Subject: [PATCH] update lsp to not report imports errors --- frontend/src/lib/components/Editor.svelte | 165 +++++++++++------- .../src/lib/components/ScriptEditor.svelte | 1 - .../src/lib/init_scripts/python_init_code.ts | 4 +- 3 files changed, 108 insertions(+), 62 deletions(-) diff --git a/frontend/src/lib/components/Editor.svelte b/frontend/src/lib/components/Editor.svelte index a5a13af657..6e0a72dad6 100644 --- a/frontend/src/lib/components/Editor.svelte +++ b/frontend/src/lib/components/Editor.svelte @@ -174,7 +174,8 @@ function createLanguageClient( transports: MessageTransports, name: string, - initializationOptions?: any + initializationOptions: any, + middlewareOptions: ((params, token, next) => any) | undefined ) { const client = new MonacoLanguageClient({ name: name, @@ -193,13 +194,11 @@ initializationOptions, middleware: { workspace: { - configuration: (params, token, configuration) => { - return [ - { - enable: true - } - ] - } + configuration: + middlewareOptions ?? + ((params, token, next) => { + return [{ enabled: true }] + }) } } }, @@ -212,7 +211,12 @@ return client } - async function connectToLanguageServer(url: string, name: string, options?: any) { + async function connectToLanguageServer( + url: string, + name: string, + initOptions: any, + middlewareOptions: any + ) { try { const webSocket = new WebSocket(url) @@ -220,7 +224,15 @@ const socket = toSocket(webSocket) const reader = new WebSocketMessageReader(socket) const writer = new WebSocketMessageWriter(socket) - const languageClient = createLanguageClient({ reader, writer }, name, options) + const languageClient = createLanguageClient( + { reader, writer }, + name, + initOptions, + middlewareOptions + ) + if (middlewareOptions != undefined) { + languageClient.registerConfigurationFeatures() + } websockets.push([languageClient, webSocket]) // HACK ALERT: for some reasons, the client need to be restarted to take into account the 'go get ' command @@ -287,63 +299,98 @@ const wsProtocol = $page.url.protocol == 'https:' ? 'wss' : 'ws' if (lang == 'typescript') { - await connectToLanguageServer(`${wsProtocol}://${$page.url.host}/ws/deno`, 'deno', { - certificateStores: null, - enablePaths: [], - config: null, - importMap: null, - internalDebug: false, - lint: false, - path: null, - tlsCertificate: null, - unsafelyIgnoreCertificateErrors: null, - unstable: true, - enable: true, - cache: null, - codeLens: { - implementations: true, - references: true - }, - suggest: { - autoImports: true, - completeFunctionCalls: false, - names: true, - paths: true, - imports: { - autoDiscover: true, - hosts: { - 'https://deno.land': true + await connectToLanguageServer( + `${wsProtocol}://${$page.url.host}/ws/deno`, + 'deno', + { + certificateStores: null, + enablePaths: [], + config: null, + importMap: null, + internalDebug: false, + lint: false, + path: null, + tlsCertificate: null, + unsafelyIgnoreCertificateErrors: null, + unstable: true, + enable: true, + cache: null, + codeLens: { + implementations: true, + references: true + }, + suggest: { + autoImports: true, + completeFunctionCalls: false, + names: true, + paths: true, + imports: { + autoDiscover: true, + hosts: { + 'https://deno.land': true + } } } - } - }) + }, + undefined + ) } else if (lang === 'python') { - await connectToLanguageServer(`${wsProtocol}://${$page.url.host}/ws/pyright`, 'pyright', { - executionEnvironments: [ - { - root: '/tmp/pyright', - pythonVersion: '3.7', - pythonPlatform: 'platform', - extraPaths: [] + await connectToLanguageServer( + `${wsProtocol}://${$page.url.host}/ws/pyright`, + 'pyright', + {}, + (params, token, next) => { + if (params.items.find((x) => x.section === 'python')) { + return [ + { + analysis: { + useLibraryCodeForTypes: true, + autoImportCompletions: true, + diagnosticSeverityOverrides: { reportMissingImports: 'none' }, + typeCheckingMode: 'basic' + } + } + ] } - ] - }) + if (params.items.find((x) => x.section === 'python.analysis')) { + return [ + { + useLibraryCodeForTypes: true, + autoImportCompletions: true, + diagnosticSeverityOverrides: { reportMissingImports: 'none' }, + typeCheckingMode: 'basic' + } + ] + } + return next(params, token) + } + ) - connectToLanguageServer(`${wsProtocol}://${$page.url.host}/ws/black`, 'black', { - formatters: { - black: { - command: 'black', - args: ['--quiet', '-'] + connectToLanguageServer( + `${wsProtocol}://${$page.url.host}/ws/black`, + 'black', + { + formatters: { + black: { + command: 'black', + args: ['--quiet', '-'] + } + }, + formatFiletypes: { + python: 'black' } }, - formatFiletypes: { - python: 'black' - } - }) + undefined + ) } else if (lang === 'go') { - connectToLanguageServer(`${wsProtocol}://${$page.url.host}/ws/go`, 'go', { - 'build.allowImplicitNetworkAccess': true - }) + connectToLanguageServer( + `${wsProtocol}://${$page.url.host}/ws/go`, + 'go', + { + 'build.allowImplicitNetworkAccess': true + }, + undefined + ) } websocketInterval && clearInterval(websocketInterval) diff --git a/frontend/src/lib/components/ScriptEditor.svelte b/frontend/src/lib/components/ScriptEditor.svelte index 121bff7e14..638c2be177 100644 --- a/frontend/src/lib/components/ScriptEditor.svelte +++ b/frontend/src/lib/components/ScriptEditor.svelte @@ -83,7 +83,6 @@ await inferArgs(lang, code, schema) validCode = true } catch (e) { - console.error("Couldn't infer args", e) validCode = false } diff --git a/frontend/src/lib/init_scripts/python_init_code.ts b/frontend/src/lib/init_scripts/python_init_code.ts index a1abbafada..cfb7d386f7 100644 --- a/frontend/src/lib/init_scripts/python_init_code.ts +++ b/frontend/src/lib/init_scripts/python_init_code.ts @@ -1,10 +1,10 @@ export default `import os import wmill -# You can import any package from PyPI, even if the assistant complains +# You can import any PyPi package. +# See here for more info: https://docs.windmill.dev/docs/advanced/imports#python # Ctrl+S to format the code. Autocompletion available. - def main( no_default: str, name="Nicolas Bourbaki",