Skip to content

Commit

Permalink
Merge pull request #138 from posit-dev/pyright-update
Browse files Browse the repository at this point in the history
Update Pyright
  • Loading branch information
wch authored May 31, 2024
2 parents f4eb380 + d2cc525 commit e7030bf
Show file tree
Hide file tree
Showing 30 changed files with 304 additions and 405 deletions.
4 changes: 0 additions & 4 deletions .prettierrc

This file was deleted.

6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,11 @@
"react-hooks/rules-of-hooks": "error",
"react-hooks/exhaustive-deps": "warn"
}
},
"prettier": {
"plugins": [
"prettier-plugin-organize-imports"
],
"organizeImportsSkipDestructiveCodeActions": true
}
}
17 changes: 4 additions & 13 deletions src/Components/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import * as React from "react";
import toast, { Toaster } from "react-hot-toast";
import type * as LSP from "vscode-languageserver-protocol";
import * as fileio from "../fileio";
import type { LanguageServerClient } from "../language-server/client";
import { createUri } from "../language-server/client";
import type { LSPClient } from "../language-server/lsp-client";
import { ensureNullClient } from "../language-server/null-client";
import { ensurePyrightClient } from "../language-server/pyright-client";
import { inferFiletype, modKeySymbol, stringToUint8Array } from "../utils";
Expand Down Expand Up @@ -112,7 +112,7 @@ export default function Editor({
// lsp-extensions.ts, and useTabbedCodeMirror.tsx, there are explicit checks
// that files are python files in order to enable LS features, and they should
// not be necessary at this level.
const lspClient: LSPClient =
const lspClient: LanguageServerClient =
appEngine === "python" ? ensurePyrightClient() : ensureNullClient();

// A unique ID for this instance of the Editor. At some point it might make
Expand Down Expand Up @@ -740,18 +740,9 @@ function fileContentsSize(files: FileContent[]): number {
*/
function diagnosticFilter(diagnostic: LSP.Diagnostic): boolean {
// Don't show diagnostics about unused vars.
if (diagnostic.severity === 4 && /is unused$/.test(diagnostic.message)) {
return false;
}

// The version of pyright we currently use still has a buggy diagnostic. Once
// we update pyright, we can remove this filter.
// https://github.com/rstudio/py-shiny/issues/124
// https://github.com/microsoft/pyright/issues/3344
if (
/Argument does not match parameter type for parameter "value".*Iterable\[SliderValueArg@input_slider\]/s.test(
diagnostic.message,
)
diagnostic.severity === 4 &&
/is not accessed$/.test(diagnostic.message)
) {
return false;
}
Expand Down
39 changes: 19 additions & 20 deletions src/Components/codeMirror/language-server/autocompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,26 @@
*
* SPDX-License-Identifier: MIT
*/
import {
createUri,
LanguageServerClient,
} from "../../../language-server/client";
import { LSPClient } from "../../../language-server/lsp-client";
import { offsetToPosition } from "./positions";
import { escapeRegExp } from "./regexp-util";
import {
autocompletion as cmAutocompletion,
Completion,
CompletionContext,
CompletionResult,
insertBracket,
type Completion,
type CompletionContext,
type CompletionResult,
} from "@codemirror/autocomplete";
import { Extension, TransactionSpec } from "@codemirror/state";
import * as LSP from "vscode-languageserver-protocol";
import type { Extension, TransactionSpec } from "@codemirror/state";
import type * as LSP from "vscode-languageserver-protocol";
import {
CompletionItem,
CompletionItemKind,
CompletionTriggerKind,
type CompletionItem,
} from "vscode-languageserver-protocol";
import {
createUri,
type LanguageServerClient,
} from "../../../language-server/client";
import { offsetToPosition } from "./positions";
import { escapeRegExp } from "./regexp-util";

// Used to find the true start of the completion. Doesn't need to exactly match
// any language's identifier definition.
Expand All @@ -32,14 +31,14 @@ const identifierLike = /[a-zA-Z0-9_\u{a1}-\u{10ffff}]+/u;
type AugmentedCompletion = Completion & { item: CompletionItem };

export function autocompletion(
lspClient: LSPClient,
filename: string
lspClient: LanguageServerClient,
filename: string,
): Extension {
const client = lspClient.client;
const client = lspClient;
const uri = createUri(filename);

const findCompletion = async (
context: CompletionContext
context: CompletionContext,
): Promise<CompletionResult | null> => {
if (!client || !uri || !client.capabilities?.completionProvider) {
return null;
Expand Down Expand Up @@ -99,7 +98,7 @@ export function autocompletion(
* Convert a LSP CompletionItem to a CM Completion object.
*/
function LSPCompletionItemToCMCompletion(
item: LSP.CompletionItem
item: LSP.CompletionItem,
): AugmentedCompletion {
const completion: AugmentedCompletion = {
// In practice we don't get textEdit fields back from Pyright so the label is used.
Expand Down Expand Up @@ -159,7 +158,7 @@ function LSPCompletionItemToCMCompletion(
// };

const createTriggerCharactersRegExp = (
client: LanguageServerClient
client: LanguageServerClient,
): RegExp | undefined => {
const characters = client.capabilities?.completionProvider?.triggerCharacters;
if (characters && characters.length > 0) {
Expand All @@ -172,7 +171,7 @@ const mapCompletionKind = Object.fromEntries(
Object.entries(CompletionItemKind).map(([key, value]) => [
value,
key.toLowerCase(),
])
]),
) as Record<CompletionItemKind, string>;

const boost = (item: LSP.CompletionItem): number | undefined => {
Expand Down
14 changes: 7 additions & 7 deletions src/Components/codeMirror/language-server/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
*
* SPDX-License-Identifier: MIT
*/
import { positionToOffset } from "./positions";
import { Diagnostic, setDiagnostics } from "@codemirror/lint";
import { EditorState, Text, Transaction } from "@codemirror/state";
import { EditorView } from "@codemirror/view";
import { setDiagnostics, type Diagnostic } from "@codemirror/lint";
import type { EditorState, Text, Transaction } from "@codemirror/state";
import type { EditorView } from "@codemirror/view";
import * as LSP from "vscode-languageserver-protocol";
import { positionToOffset } from "./positions";

/// An action associated with a diagnostic.
export interface Action {
Expand All @@ -29,7 +29,7 @@ const severityMapping = {

export const diagnosticsMapping = (
document: Text,
lspDiagnostics: LSP.Diagnostic[]
lspDiagnostics: LSP.Diagnostic[],
): Diagnostic[] =>
lspDiagnostics
.map(({ range, message, severity, tags }): Diagnostic | undefined => {
Expand All @@ -56,12 +56,12 @@ export const diagnosticsMapping = (
*/
export function diagnosticToTransaction(
editorState: EditorState,
lspDiagnostics: LSP.Diagnostic[]
lspDiagnostics: LSP.Diagnostic[],
): Transaction {
const diagnostics = diagnosticsMapping(editorState.doc, lspDiagnostics);

const diagnosticsTransaction = editorState.update(
setDiagnostics(editorState, diagnostics)
setDiagnostics(editorState, diagnostics),
);

return diagnosticsTransaction;
Expand Down
28 changes: 15 additions & 13 deletions src/Components/codeMirror/language-server/hover.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
import type { Extension } from "@codemirror/state";
import { hoverTooltip, type Tooltip } from "@codemirror/view";
import {
HoverRequest,
type HoverParams,
type MarkupContent,
} from "vscode-languageserver-protocol";
import {
createUri,
LanguageServerClient,
type LanguageServerClient,
} from "../../../language-server/client";
import { LSPClient } from "../../../language-server/lsp-client";
import { renderDocumentation } from "./documentation";
import { offsetToPosition } from "./positions";
import { Extension } from "@codemirror/state";
import { hoverTooltip, Tooltip } from "@codemirror/view";
import {
HoverParams,
HoverRequest,
MarkupContent,
} from "vscode-languageserver-protocol";

export function hover(lspClient: LSPClient, filename: string): Extension {
return createHoverTooltip(lspClient.client, filename);
export function hover(
lspClient: LanguageServerClient,
filename: string,
): Extension {
return createHoverTooltip(lspClient, filename);
}

function createHoverTooltip(
client: LanguageServerClient,
filename: string
filename: string,
): Extension {
const uri = createUri(filename);

Expand All @@ -33,7 +35,7 @@ function createHoverTooltip(

const result = await client.connection.sendRequest(
HoverRequest.type,
params
params,
);
if (result === null) return null;

Expand Down
14 changes: 8 additions & 6 deletions src/Components/codeMirror/language-server/lsp-extension.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { LSPClient } from "../../../language-server/lsp-client";
import type { Extension } from "@codemirror/state";
import { EditorView, type ViewUpdate } from "@codemirror/view";
import type { TextDocumentContentChangeEvent } from "vscode-languageserver-protocol";
import type { LanguageServerClient } from "../../../language-server/client";
import { inferFiletype } from "../../../utils";
import { autocompletion } from "./autocompletion";
import { hover } from "./hover";
import { offsetToPosition } from "./positions";
import { signatureHelp } from "./signatureHelp";
import { Extension } from "@codemirror/state";
import { EditorView, ViewUpdate } from "@codemirror/view";
import { TextDocumentContentChangeEvent } from "vscode-languageserver-protocol";

export function languageServerExtensions(
lspClient: LSPClient,
filename: string
lspClient: LanguageServerClient,
filename: string,
): Extension[] {
if (inferFiletype(filename) !== "python") {
return [];
Expand Down Expand Up @@ -44,6 +44,7 @@ export function languageServerExtensions(

if (nChanges === 1) {
// If we had exactly one change, send it to the Language Server.
// eslint-disable-next-line @typescript-eslint/no-floating-promises
lspClient.changeFile(filename, changeEvent);
} else {
// If we had more than one change (because of multiple cursors), don't
Expand All @@ -54,6 +55,7 @@ export function languageServerExtensions(
// expects the changes to be recordered in order, which means that it
// expects the cursor positions at t0, t1, t2, etc. These two schemes
// aren't compatible, so we'll just send the entire document.
// eslint-disable-next-line @typescript-eslint/no-floating-promises
lspClient.changeFile(filename, { text: u.view.state.doc.toString() });
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Components/codeMirror/language-server/positions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
*
* SPDX-License-Identifier: MIT
*/
import { Text } from "@codemirror/state";
import { Position, Range } from "vscode-languageserver-protocol";
import type { Text } from "@codemirror/state";
import type { Position, Range } from "vscode-languageserver-protocol";

// See https://microsoft.github.io/language-server-protocol/specifications/specification-3-17/#position

export const positionToOffset = (
document: Text,
position: Position
position: Position,
): number | undefined => {
if (position.line >= document.lines) {
return undefined;
Expand Down
Loading

0 comments on commit e7030bf

Please sign in to comment.