Skip to content

Commit

Permalink
switch to ruff for better perf
Browse files Browse the repository at this point in the history
  • Loading branch information
noneofyourbusiness1415252 committed Nov 12, 2023
1 parent 68c5e44 commit 3ad488d
Show file tree
Hide file tree
Showing 8 changed files with 779 additions and 2,009 deletions.
4 changes: 2 additions & 2 deletions packages/pyright-internal/src/languageServerBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ import {
import { ResultProgressReporter, attachWorkDone } from 'vscode-languageserver/lib/common/progress';

import { TextDocument } from 'vscode-languageserver-textdocument';
import { formatBufferWithYapf } from '../../pyright-yapf';
import { formatBuffer } from '../../pyright-yapf';
import { AnalysisResults } from './analyzer/analysis';
import { BackgroundAnalysisProgram, InvalidatedReason } from './analyzer/backgroundAnalysisProgram';
import { ImportResolver } from './analyzer/importResolver';
Expand Down Expand Up @@ -849,7 +849,7 @@ export abstract class LanguageServerBase implements LanguageServerInterface, Dis

const buf = workspace.service.getSourceFile(filePath)?.getOpenFileContents();
if (!buf) return;
return formatBufferWithYapf(buf, params.options.tabSize, !params.options.insertSpaces);
return formatBuffer(buf, params.options.tabSize, !params.options.insertSpaces);
}

protected async onDeclaration(
Expand Down
59 changes: 23 additions & 36 deletions packages/pyright-yapf/index.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,25 @@
import { TextEdit, uinteger } from 'vscode-languageserver';
import { writeFile, open } from "fs/promises";
import { interpreter, PyModule } from "node-calls-python";
let yapf: PyModule;
const YAPF_CONF = `[style]
indent_width: `;
const style_config = "/dev/shm/yapf.ini";
(async()=>{yapf = await interpreter.import("yapf.yapflib.yapf_api", false);
await writeFile(style_config, YAPF_CONF);})();
async function _runYapf(buf: string, indentWidth: number, useTabs: boolean): Promise<string[]> {
const handle = await open(style_config, "a+");
await handle.truncate(YAPF_CONF.length);
await handle.write(`${indentWidth}`);
if (useTabs)
await handle.write(`
use_tabs: true`);
return interpreter.call(yapf, "FormatCode", buf, {style_config, __kwargs: true}) as unknown as string[];
}
export async function formatBufferWithYapf(buf: string, indentWidth: number, useTabs: boolean): Promise<TextEdit[]> {
const [newText, changed] = await _runYapf(buf, indentWidth, useTabs);
const changes = [];
if (changed)
changes.push({
// range may seem sus but this is what the official ruff lsp actually does https://github.com/astral-sh/ruff-lsp/blob/main/ruff_lsp/server.py#L735-L740
range: {
start: {
line: 0,
character: 0,
},
end: {
line: uinteger.MAX_VALUE,
character: 0,
},
},
newText,
})
return changes;
import { readFile } from "fs/promises";
import init, { format } from "@wasm-fmt/ruff_fmt";
/** @deprecated formatBuffer */
export const formatBufferWithYapf = formatBuffer;
export async function formatBuffer(buf: string, indent_width: number, useTabs: boolean): Promise<TextEdit[]> {
const newText = format(buf, "", {
indent_style: useTabs? "tab" : "space", indent_width });
return [
{
// range may seem sus but this is what the official ruff lsp actually does https://github.com/astral-sh/ruff-lsp/blob/main/ruff_lsp/server.py#L735-L740
range: {
start: {
line: 0,
character: 0,
},
end: {
line: uinteger.MAX_VALUE,
character: 0,
},
},
newText,
},
];
}
11 changes: 11 additions & 0 deletions packages/pyright-yapf/package-lock.json

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

3 changes: 2 additions & 1 deletion packages/pyright-yapf/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"vscode-languageserver": "8.1.0",
"@wasm-fmt/ruff_fmt": "^0.5.0",
"node-calls-python": "^1.8.0",
"vscode-languageserver": "8.1.0",
"vscode-uri": "^3.0.7"
},
"files": [
Expand Down
1 change: 1 addition & 0 deletions packages/pyright-yapf/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"extends": "../../tsconfig.json",
"target": "es2017",
"compilerOptions": {
"outDir": "./out"
},
Expand Down
13 changes: 12 additions & 1 deletion packages/pyright/langserver.index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,16 @@

// Stash the base directory into a global variable.
global.__rootDirectory = __dirname + '/dist/';

const {readFile} = require("fs/promises");
oldFetch = global.fetch;
/**Workaround to get WASM loading to work*/
global.fetch = async function(url) {
try {
let res = new Response(await readFile(url.pathname));
// node doesn't support passing headers in the options param?
res.headers.set("Content-Type", "application/wasm");
return res;
} catch {}
return oldFetch(url);
};
require('./dist/pyright-langserver');
Loading

0 comments on commit 3ad488d

Please sign in to comment.