-
Notifications
You must be signed in to change notification settings - Fork 16
Add Python example with pyright #99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
kazk
wants to merge
1
commit into
main
Choose a base branch
from
pyright-example
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,26 @@ | ||
| # Logs | ||
| logs | ||
| *.log | ||
| npm-debug.log* | ||
| yarn-debug.log* | ||
| yarn-error.log* | ||
| pnpm-debug.log* | ||
| lerna-debug.log* | ||
|
|
||
| node_modules | ||
| dist | ||
| dist-ssr | ||
| *.local | ||
|
|
||
| # Editor directories and files | ||
| .vscode/* | ||
| !.vscode/extensions.json | ||
| .idea | ||
| .DS_Store | ||
| *.suo | ||
| *.ntvs* | ||
| *.njsproj | ||
| *.sln | ||
| *.sw? | ||
|
|
||
| bin/ |
This file contains hidden or 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,20 @@ | ||
| # @qualified/codemirror-workspace Python demo | ||
|
|
||
| --- | ||
|
|
||
| Install [`pnpm`] if you don't have it installed: | ||
|
|
||
| ``` | ||
| npm i -g pnpm@6 | ||
| ``` | ||
|
|
||
| Install [`lsp-ws-proxy`](https://github.com/qualified/lsp-ws-proxy) (v0.8.0+ is required) by downloading a binary from [releases](https://github.com/qualified/lsp-ws-proxy/releases) and moving it in `PATH` or `./bin`. | ||
|
|
||
| Run the following to start a dev server: | ||
|
|
||
| ```bash | ||
| pnpm install | ||
| pnpm dev | ||
| ``` | ||
|
|
||
| [`pnpm`]: https://pnpm.js.org/ |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or 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,18 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8" /> | ||
| <link rel="icon" type="image/svg+xml" href="favicon.svg" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
| <title>CodeMirror Workspace Python Demo</title> | ||
| </head> | ||
| <body> | ||
| <div class="editors"> | ||
| <div id="preloaded-editor" class="editor"></div> | ||
| <div id="solution-editor" class="editor"></div> | ||
| <div id="test-editor" class="editor"></div> | ||
| </div> | ||
|
|
||
| <script type="module" src="/src/main.ts"></script> | ||
| </body> | ||
| </html> |
This file contains hidden or 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,24 @@ | ||
| { | ||
| "name": "@qualified/codemirror-workspace-demo-python", | ||
| "version": "0.0.1", | ||
| "private": true, | ||
| "scripts": { | ||
| "dev": "concurrently 'vite' 'pnpm start-proxy'", | ||
| "build": "tsc && vite build", | ||
| "preview": "concurrently 'vite preview' 'pnpm start-proxy'", | ||
| "start-proxy": "cd workspace && PATH=$(pwd)/../bin:$PATH RUST_LOG=info,lsp_ws_proxy=debug lsp-ws-proxy --remap -l 9990 -- pyright-langserver --stdio" | ||
| }, | ||
| "dependencies": { | ||
| "@qualified/codemirror-workspace": "workspace:0.5.0", | ||
| "codemirror": "^5.59.2", | ||
| "marked": "^4.0.17" | ||
| }, | ||
| "devDependencies": { | ||
| "@types/codemirror": "^5.0.0", | ||
| "@types/marked": "^4.0.3", | ||
| "concurrently": "^7.2.2", | ||
| "pyright": "^1.1.271", | ||
| "typescript": "^4.7.4", | ||
| "vite": "^2.9.9" | ||
| } | ||
| } |
This file contains hidden or 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,122 @@ | ||
| import "./style.css"; | ||
|
|
||
| import CodeMirror from "codemirror"; | ||
| import "codemirror/mode/python/python"; | ||
| import "codemirror/lib/codemirror.css"; | ||
| import "codemirror/theme/idea.css"; | ||
| // ShowHint addon is required for completion capability. | ||
| import "codemirror/addon/hint/show-hint.css"; | ||
| import "codemirror/addon/hint/show-hint"; | ||
| import "codemirror/addon/edit/matchbrackets"; | ||
| import "codemirror/addon/edit/closebrackets"; | ||
| import "codemirror/addon/runmode/runmode"; | ||
| // import "codemirror/keymap/vim"; | ||
|
|
||
| import { marked } from "marked"; | ||
|
|
||
| import { Workspace } from "@qualified/codemirror-workspace"; | ||
| import "@qualified/codemirror-workspace/css/default.css"; | ||
|
|
||
| import preloadedPy from "../workspace/preloaded.py?raw"; | ||
| import solutionPy from "../workspace/solution.py?raw"; | ||
| import solutionTestPy from "../workspace/tests/test_solution.py?raw"; | ||
|
|
||
| const modeMap: { [k: string]: string } = { | ||
| python: "python", | ||
| }; | ||
|
|
||
| const highlight = (code: string, language: string) => { | ||
| const mode = modeMap[language] || "text/plain"; | ||
| const tmp = document.createElement("div"); | ||
| CodeMirror.runMode(code, mode, tmp, { tabSize: 4 }); | ||
| return tmp.innerHTML; | ||
| }; | ||
|
|
||
| marked.use({ | ||
| // @ts-ignore renderer can be object literal | ||
| renderer: { | ||
| code(code: string, language: string | undefined) { | ||
| if (!language) language = "text"; | ||
| code = highlight(code, language); | ||
| // We need to add a class for the theme (e.g., `cm-s-idea`) on the wrapper. | ||
| // If we're using a custom theme, it can apply its styles to `code[class^="language-"]` | ||
| // and use Marked's default `code` with `highlight` option. | ||
| return `<pre><code class="cm-s-idea language-${language}">${code}</code></pre>`; | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| const $ = (sel: string) => { | ||
| const el = document.querySelector(sel); | ||
| if (!el) throw new Error(`No element matching ${sel}`); | ||
| return el as HTMLElement; | ||
| }; | ||
|
|
||
| const config: CodeMirror.EditorConfiguration = { | ||
| theme: "idea", | ||
| // keyMap: "vim", | ||
| gutters: ["cmw-gutter"], | ||
| lineNumbers: true, | ||
| matchBrackets: true, | ||
| autoCloseBrackets: true, | ||
| indentUnit: 4, | ||
| }; | ||
|
|
||
| const workspace = new Workspace({ | ||
| rootUri: "source://", | ||
| getLanguageAssociation: (uri: string) => { | ||
| if (uri.endsWith(".py")) { | ||
| return { | ||
| languageId: "python", | ||
| languageServerIds: ["pyright-langserver"], | ||
| }; | ||
| } | ||
| // Workspace will ignore the file if null is returned. | ||
| return null; | ||
| }, | ||
| getConnectionString: async (id: string) => { | ||
| return id ? `ws://localhost:9990?name=${id}` : ""; | ||
| }, | ||
| // Support Markdown documentation | ||
| renderMarkdown: (markdown) => marked(markdown), | ||
| configs: { | ||
| "pyright-langserver": { | ||
| settings: { | ||
| python: { | ||
| analysis: { | ||
| autoSearchPaths: true, | ||
| useLibraryCodeForTypes: true, | ||
| diagnosticMode: "openFiles", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| workspace.openTextDocument( | ||
| "preloaded.py", | ||
| CodeMirror($("#preloaded-editor"), { | ||
| ...config, | ||
| mode: "python", | ||
| value: preloadedPy, | ||
| }) | ||
| ); | ||
|
|
||
| workspace.openTextDocument( | ||
| "solution.py", | ||
| CodeMirror($("#solution-editor"), { | ||
| ...config, | ||
| mode: "python", | ||
| value: solutionPy, | ||
| }) | ||
| ); | ||
|
|
||
| workspace.openTextDocument( | ||
| "tests/solution_test.py", | ||
| CodeMirror($("#test-editor"), { | ||
| ...config, | ||
| mode: "python", | ||
| value: solutionTestPy, | ||
| }) | ||
| ); |
This file contains hidden or 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,21 @@ | ||
| .CodeMirror { | ||
| height: 100% !important; | ||
| } | ||
|
|
||
| .editor { | ||
| width: 100%; | ||
| height: 220px; | ||
| overflow: auto; | ||
| border-bottom: 1px solid #ccc; | ||
| } | ||
|
|
||
| .editors { | ||
| padding: 2rem; | ||
| margin: 0 auto; | ||
| max-width: 80ch; | ||
| } | ||
|
|
||
| .buttons { | ||
| margin: 0 auto; | ||
| text-align: center; | ||
| } |
This file contains hidden or 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 @@ | ||
| /// <reference types="vite/client" /> |
This file contains hidden or 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,20 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "target": "ESNext", | ||
| "useDefineForClassFields": true, | ||
| "module": "ESNext", | ||
| "lib": ["ESNext", "DOM"], | ||
| "moduleResolution": "Node", | ||
| "strict": true, | ||
| "sourceMap": true, | ||
| "resolveJsonModule": true, | ||
| "isolatedModules": true, | ||
| "esModuleInterop": true, | ||
| "noEmit": true, | ||
| "noUnusedLocals": true, | ||
| "noUnusedParameters": true, | ||
| "noImplicitReturns": true, | ||
| "skipLibCheck": true | ||
| }, | ||
| "include": ["src"] | ||
| } |
This file contains hidden or 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,4 @@ | ||
| # preloaded.py | ||
| def helper(a, b): | ||
| """ A helper doc. """ | ||
| return a * b |
This file contains hidden or 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,8 @@ | ||
| { | ||
| "include": ["./"], | ||
| "executionEnvironments": [ | ||
| { | ||
| "root": "./" | ||
| } | ||
| ] | ||
| } |
This file contains hidden or 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,7 @@ | ||
| # solution.py | ||
| from preloaded import helper | ||
|
|
||
|
|
||
| def add(a, b): | ||
| print(helper(a, b)) | ||
| return a + b |
This file contains hidden or 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,10 @@ | ||
| # tests/test_solution.py | ||
| import unittest | ||
|
|
||
| # TODO Fix `solution` not resolved | ||
| from solution import add | ||
|
|
||
|
|
||
| class TestAddition(unittest.TestCase): | ||
| def test_add(self): | ||
| self.assertEqual(add(1, 1), 2) | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't figure out how to make this resolved. It resolves fine in VS Code using pyright.
pyrightconfig.jsonis documented in https://github.com/microsoft/pyright/blob/main/docs/configuration.md