-
-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
254 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
|
||
<head> | ||
<title>Monaco Language Client Python Example</title> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
</head> | ||
|
||
<body> | ||
<h2>Monaco Language Client Python Example</h2> | ||
<div id="container" style="width:800px;height:600px;border:1px solid grey"></div> | ||
<script type="module" src="./src/python/client.ts"></script> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* -------------------------------------------------------------------------------------------- | ||
* Copyright (c) 2018-2022 TypeFox GmbH (http://www.typefox.io). All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
* ------------------------------------------------------------------------------------------ */ | ||
|
||
import 'monaco-editor/esm/vs/editor/editor.all.js'; | ||
import 'monaco-editor/esm/vs/editor/standalone/browser/iPadShowKeyboard/iPadShowKeyboard.js'; | ||
import * as monaco from 'monaco-editor/esm/vs/editor/editor.api.js'; | ||
import * as vscode from 'vscode'; | ||
import 'vscode/default-extensions/theme-defaults'; | ||
import 'vscode/default-extensions/python'; | ||
import { updateUserConfiguration } from 'vscode/service-override/configuration'; | ||
import { createConfiguredEditor, createModelReference } from 'vscode/monaco'; | ||
import { initServices, MonacoLanguageClient } from 'monaco-languageclient'; | ||
import { CloseAction, ErrorAction, MessageTransports } from 'vscode-languageclient'; | ||
import { WebSocketMessageReader, WebSocketMessageWriter, toSocket } from 'vscode-ws-jsonrpc'; | ||
import { RegisteredFileSystemProvider, registerFileSystemOverlay, RegisteredMemoryFile } from 'vscode/service-override/files'; | ||
|
||
import { buildWorkerDefinition } from 'monaco-editor-workers'; | ||
buildWorkerDefinition('../../../node_modules/monaco-editor-workers/dist/workers/', new URL('', window.location.href).href, false); | ||
|
||
const createWebSocket = (url: string): WebSocket => { | ||
const webSocket = new WebSocket(url); | ||
webSocket.onopen = () => { | ||
const socket = toSocket(webSocket); | ||
const reader = new WebSocketMessageReader(socket); | ||
const writer = new WebSocketMessageWriter(socket); | ||
const languageClient = createLanguageClient({ | ||
reader, | ||
writer | ||
}); | ||
languageClient.start(); | ||
reader.onClose(() => languageClient.stop()); | ||
}; | ||
return webSocket; | ||
}; | ||
|
||
const createLanguageClient = (transports: MessageTransports): MonacoLanguageClient => { | ||
return new MonacoLanguageClient({ | ||
name: 'Pyright Language Client', | ||
clientOptions: { | ||
// use a language id as a document selector | ||
documentSelector: ['json'], | ||
// disable the default error handler | ||
errorHandler: { | ||
error: () => ({ action: ErrorAction.Continue }), | ||
closed: () => ({ action: CloseAction.DoNotRestart }) | ||
}, | ||
synchronize: { | ||
fileEvents: [vscode.workspace.createFileSystemWatcher('**')] | ||
} | ||
}, | ||
// create a language client connection from the JSON RPC connection on demand | ||
connectionProvider: { | ||
get: () => { | ||
return Promise.resolve(transports); | ||
} | ||
} | ||
}); | ||
}; | ||
|
||
const run = async () => { | ||
// init vscode-api | ||
await initServices({ | ||
enableModelService: true, | ||
enableThemeService: true, | ||
enableTextmateService: true, | ||
enableKeybindingsService: true, | ||
configureConfigurationServiceConfig: { | ||
defaultWorkspaceUri: '/tmp' | ||
}, | ||
debugLogging: true | ||
}); | ||
|
||
updateUserConfiguration(`{ | ||
"editor.fontSize": 14, | ||
"workbench.colorTheme": "Default Dark Modern" | ||
}`); | ||
|
||
const fileSystemProvider = new RegisteredFileSystemProvider(false); | ||
fileSystemProvider.registerFile(new RegisteredMemoryFile(vscode.Uri.file('/tmp/hello.py'), 'print("Hello, World!")')); | ||
registerFileSystemOverlay(1, fileSystemProvider); | ||
|
||
// create the web socket and configure to start the language client on open | ||
createWebSocket('ws://localhost:30000/pyright'); | ||
|
||
// register the JSON language with Monaco | ||
const languageId = 'python'; | ||
monaco.languages.register({ | ||
id: languageId, | ||
extensions: ['.py'], | ||
aliases: ['PYTHON', 'python'] | ||
}); | ||
|
||
// create the model inside the workspace | ||
const modelRef = await createModelReference(monaco.Uri.file('/tmp/hello.py')); | ||
modelRef.object.setLanguageId(languageId); | ||
|
||
// create monaco editor | ||
createConfiguredEditor(document.getElementById('container')!, { | ||
model: modelRef.object.textEditorModel, | ||
automaticLayout: true | ||
}); | ||
}; | ||
|
||
run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
|
||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Pyright Python Language Server</title> | ||
</head> | ||
|
||
<body> | ||
<h1>Hello Pyright</h1> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* -------------------------------------------------------------------------------------------- | ||
* Copyright (c) 2018-2022 TypeFox GmbH (http://www.typefox.io). All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
* ------------------------------------------------------------------------------------------ */ | ||
import { WebSocketServer } from 'ws'; | ||
import { IncomingMessage } from 'http'; | ||
import { URL } from 'url'; | ||
import { Socket } from 'net'; | ||
import express from 'express'; | ||
import { IWebSocket, WebSocketMessageReader, WebSocketMessageWriter } from 'vscode-ws-jsonrpc'; | ||
import { createConnection, createServerProcess, forward } from 'vscode-ws-jsonrpc/server'; | ||
import { Message, InitializeRequest, InitializeParams } from 'vscode-languageserver'; | ||
import { getLocalDirectory } from '../server/fs-utils.js'; | ||
|
||
const launchLanguageServer = (socket: IWebSocket) => { | ||
const reader = new WebSocketMessageReader(socket); | ||
const writer = new WebSocketMessageWriter(socket); | ||
|
||
// start the language server as an external process | ||
const socketConnection = createConnection(reader, writer, () => socket.dispose()); | ||
const serverConnection = createServerProcess('PYRIGHT', 'npx', ['pyright', '--verbose', '--watch', '/tmp']); | ||
if (serverConnection) { | ||
forward(socketConnection, serverConnection, message => { | ||
if (Message.isRequest(message)) { | ||
console.log(message); | ||
if (message.method === InitializeRequest.type.method) { | ||
const initializeParams = message.params as InitializeParams; | ||
initializeParams.processId = process.pid; | ||
} | ||
} | ||
return message; | ||
}); | ||
} | ||
}; | ||
|
||
const run = () => { | ||
process.on('uncaughtException', function(err: any) { | ||
console.error('Uncaught Exception: ', err.toString()); | ||
if (err.stack) { | ||
console.error(err.stack); | ||
} | ||
}); | ||
|
||
// create the express application | ||
const app = express(); | ||
// server the static content, i.e. index.html | ||
const dir = getLocalDirectory(import.meta.url); | ||
app.use(express.static(dir)); | ||
// start the server | ||
const server = app.listen(30000); | ||
// create the web socket | ||
const wss = new WebSocketServer({ | ||
noServer: true, | ||
perMessageDeflate: false | ||
}); | ||
server.on('upgrade', (request: IncomingMessage, socket: Socket, head: Buffer) => { | ||
const baseURL = `http://${request.headers.host}/`; | ||
const pathname = request.url ? new URL(request.url, baseURL).pathname : undefined; | ||
if (pathname === '/pyright') { | ||
wss.handleUpgrade(request, socket, head, webSocket => { | ||
const socket: IWebSocket = { | ||
send: content => webSocket.send(content, error => { | ||
if (error) { | ||
throw error; | ||
} | ||
}), | ||
onMessage: cb => webSocket.on('message', (data) => { | ||
console.log(data.toString()); | ||
cb(data); | ||
}), | ||
onError: cb => webSocket.on('error', cb), | ||
onClose: cb => webSocket.on('close', cb), | ||
dispose: () => webSocket.close() | ||
}; | ||
// launch the server when the web socket is opened | ||
if (webSocket.readyState === webSocket.OPEN) { | ||
launchLanguageServer(socket); | ||
} else { | ||
webSocket.on('open', () => launchLanguageServer(socket)); | ||
} | ||
}); | ||
} | ||
}); | ||
}; | ||
|
||
run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
print("Hello, World!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters