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 Dec 2, 2023
1 parent aecba1a commit 6ea6f08
Show file tree
Hide file tree
Showing 8 changed files with 8,647 additions and 2,201 deletions.
10,742 changes: 8,588 additions & 2,154 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 2 additions & 3 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 @@ -812,7 +812,6 @@ export abstract class LanguageServerBase implements LanguageServerInterface, Dis
}

protected onDidChangeConfiguration(params: DidChangeConfigurationParams) {

this.console.log(`Received updated settings`);
if (params?.settings) {
this.defaultClientConfig = params?.settings;
Expand Down Expand Up @@ -849,7 +848,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
64 changes: 27 additions & 37 deletions packages/pyright-yapf/index.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,29 @@
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[];
>>>>>>> 68c5e44b6 (efficient async formatting)
import init, { format } from '@wasm-fmt/ruff_fmt';
(async () => {
await init();
})();
/** @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,
},
];
}
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;
}
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 {}

Check failure on line 16 in packages/pyright/langserver.index.js

View workflow job for this annotation

GitHub Actions / Style

Empty block statement
return oldFetch(url);
};
require('./dist/pyright-langserver');
9 changes: 4 additions & 5 deletions packages/pyright/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ const typeshedFallback = path.resolve(__dirname, '..', 'pyright-internal', 'type
/**@type {(env: any, argv: { mode: 'production' | 'development' | 'none' }) => import('webpack').Configuration}*/
module.exports = (_, { mode }) => {
return {
node: {
__dirname: false,
},
context: __dirname,
entry: {
pyright: './src/pyright.ts',
Expand All @@ -36,7 +39,7 @@ module.exports = (_, { mode }) => {
timings: true,
},
resolve: {
extensions: ['.ts', '.js', '.node'],
extensions: ['.ts', '.js'],
alias: tsconfigResolveAliases('tsconfig.json'),
},
externals: {
Expand All @@ -61,10 +64,6 @@ module.exports = (_, { mode }) => {
target: 'node12',
},
},
{
test: /\.node$/,
loader: 'node-loader',
}
],
},
plugins: [new CopyPlugin({ patterns: [{ from: typeshedFallback, to: 'typeshed-fallback' }] })],
Expand Down

0 comments on commit 6ea6f08

Please sign in to comment.