Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions examples/pyright/.gitignore
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/
20 changes: 20 additions & 0 deletions examples/pyright/README.md
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/
15 changes: 15 additions & 0 deletions examples/pyright/favicon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions examples/pyright/index.html
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>
24 changes: 24 additions & 0 deletions examples/pyright/package.json
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"
}
}
122 changes: 122 additions & 0 deletions examples/pyright/src/main.ts
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,
})
);
21 changes: 21 additions & 0 deletions examples/pyright/src/style.css
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;
}
1 change: 1 addition & 0 deletions examples/pyright/src/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="vite/client" />
20 changes: 20 additions & 0 deletions examples/pyright/tsconfig.json
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"]
}
4 changes: 4 additions & 0 deletions examples/pyright/workspace/preloaded.py
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
8 changes: 8 additions & 0 deletions examples/pyright/workspace/pyrightconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"include": ["./"],
"executionEnvironments": [
{
"root": "./"
}
]
}
7 changes: 7 additions & 0 deletions examples/pyright/workspace/solution.py
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
10 changes: 10 additions & 0 deletions examples/pyright/workspace/tests/test_solution.py
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
Comment on lines +4 to +5
Copy link
Member Author

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.json is documented in https://github.com/microsoft/pyright/blob/main/docs/configuration.md



class TestAddition(unittest.TestCase):
def test_add(self):
self.assertEqual(add(1, 1), 2)
29 changes: 29 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.