Skip to content

Commit

Permalink
Added new language server setting pyright.disableTaggedHints to dis…
Browse files Browse the repository at this point in the history
…able the use of diagnostics hints with tags. Some language server clients do not display these tagged hints in the intended manner and instead treat them as regular diagnostics.
  • Loading branch information
erictraut committed Jan 15, 2024
1 parent 4f9ecaa commit cdcd404
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 5 deletions.
2 changes: 2 additions & 0 deletions docs/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ The Pyright language server honors the following settings.

**pyright.disableOrganizeImports** [boolean]: Disables the “Organize Imports” command. This is useful if you are using another extension that provides similar functionality and you don’t want the two extensions to fight each other.

**pyright.disableTaggedHints** [boolean]: Disables the use of hint diagnostics with special tags to tell the client to display text ranges in a "grayed out" manner (to indicate unreachable code or unreferenced symbols) or in a "strike through" manner (to indicate use of a deprecated feature).

**pyright.openFilesOnly** [boolean]: Determines whether pyright analyzes (and reports errors for) all files in the workspace, as indicated by the config file. If this option is set to true, pyright analyzes only open files. This setting is deprecated in favor of python.analysis.diagnosticMode. It will be removed at a future time.

**pyright.useLibraryCodeForTypes** [boolean]: This setting is deprecated in favor of python.analysis.useLibraryCodeForTypes. It will be removed at a future time.
Expand Down
17 changes: 12 additions & 5 deletions packages/pyright-internal/src/analyzer/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { ConfigOptions, ExecutionEnvironment, matchFileSpecs } from '../common/c
import { ConsoleInterface, StandardConsole } from '../common/console';
import * as debug from '../common/debug';
import { assert } from '../common/debug';
import { Diagnostic } from '../common/diagnostic';
import { Diagnostic, DiagnosticCategory } from '../common/diagnostic';
import { FileDiagnostics } from '../common/diagnosticSink';
import { FileEditAction } from '../common/editAction';
import { EditableProgram, ProgramView } from '../common/extensibility';
Expand Down Expand Up @@ -889,11 +889,18 @@ export class Program {

this._sourceFileList.forEach((sourceFileInfo) => {
if (this._shouldCheckFile(sourceFileInfo)) {
const diagnostics = sourceFileInfo.sourceFile.getDiagnostics(
options,
sourceFileInfo.diagnosticsVersion
);
let diagnostics = sourceFileInfo.sourceFile.getDiagnostics(options, sourceFileInfo.diagnosticsVersion);
if (diagnostics !== undefined) {
// Filter out all categories that are translated to tagged hints?
if (options.disableTaggedHints) {
diagnostics = diagnostics.filter(
(diag) =>
diag.category !== DiagnosticCategory.UnreachableCode &&
diag.category !== DiagnosticCategory.UnusedCode &&
diag.category !== DiagnosticCategory.Deprecated
);
}

fileDiagnostics.push({
fileUri: sourceFileInfo.sourceFile.getUri(),
version: sourceFileInfo.sourceFile.getClientVersion(),
Expand Down
2 changes: 2 additions & 0 deletions packages/pyright-internal/src/analyzer/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,8 @@ export class AnalyzerService {

this._configFileUri = configFilePath || pyprojectFilePath;

configOptions.disableTaggedHints = !!commandLineOptions.disableTaggedHints;

// If we found a config file, parse it to compute the effective options.
let configJsonObj: object | undefined;
if (configFilePath) {
Expand Down
3 changes: 3 additions & 0 deletions packages/pyright-internal/src/common/commandLineOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ export class CommandLineOptions {
// Analyze functions and methods that have no type annotations?
analyzeUnannotatedFunctions?: boolean;

// Disable reporting of hint diagnostics with tags?
disableTaggedHints?: boolean;

constructor(executionRoot: string, fromVsCodeExtension: boolean) {
this.executionRoot = executionRoot;
this.fromVsCodeExtension = fromVsCodeExtension;
Expand Down
3 changes: 3 additions & 0 deletions packages/pyright-internal/src/common/configOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,9 @@ export class ConfigOptions {
// Was this config initialized from JSON (pyrightconfig/pyproject)?
initializedFromJson = false;

// Filter out any hint diagnostics with tags?
disableTaggedHints = false;

//---------------------------------------------------------------
// Diagnostics Rule Set

Expand Down
2 changes: 2 additions & 0 deletions packages/pyright-internal/src/languageServerBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ export interface ServerSettings {
typeCheckingMode?: string | undefined;
useLibraryCodeForTypes?: boolean | undefined;
disableLanguageServices?: boolean | undefined;
disableTaggedHints?: boolean | undefined;
disableOrganizeImports?: boolean | undefined;
autoSearchPaths?: boolean | undefined;
extraPaths?: Uri[] | undefined;
Expand Down Expand Up @@ -509,6 +510,7 @@ export abstract class LanguageServerBase implements LanguageServerInterface, Dis
this.updateOptionsAndRestartService(workspace, serverSettings);

workspace.disableLanguageServices = !!serverSettings.disableLanguageServices;
workspace.disableTaggedHints = !!serverSettings.disableTaggedHints;
workspace.disableOrganizeImports = !!serverSettings.disableOrganizeImports;

// Don't use workspace.isInitialized directly since it might have been
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export class AnalyzerServiceExecutor {
options.fileSystem
),
disableLanguageServices: true,
disableTaggedHints: true,
disableOrganizeImports: true,
disableWorkspaceSymbol: true,
isInitialized: createInitStatus(),
Expand Down Expand Up @@ -103,6 +104,7 @@ function getEffectiveCommandLineOptions(
commandLineOptions.typeEvaluationTimeThreshold = serverSettings.typeEvaluationTimeThreshold ?? 50;
commandLineOptions.enableAmbientAnalysis = trackFiles;
commandLineOptions.pythonEnvironmentName = pythonEnvironmentName;
commandLineOptions.disableTaggedHints = serverSettings.disableTaggedHints;

if (!trackFiles) {
commandLineOptions.watchForSourceChanges = false;
Expand Down
2 changes: 2 additions & 0 deletions packages/pyright-internal/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export class PyrightServer extends LanguageServerBase {
openFilesOnly: true,
useLibraryCodeForTypes: true,
disableLanguageServices: false,
disableTaggedHints: false,
disableOrganizeImports: false,
typeCheckingMode: 'standard',
diagnosticSeverityOverrides: {},
Expand Down Expand Up @@ -205,6 +206,7 @@ export class PyrightServer extends LanguageServerBase {
}

serverSettings.disableLanguageServices = !!pyrightSection.disableLanguageServices;
serverSettings.disableTaggedHints = !!pyrightSection.disableTaggedHints;
serverSettings.disableOrganizeImports = !!pyrightSection.disableOrganizeImports;

const typeCheckingMode = pyrightSection.typeCheckingMode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export class TestLanguageService implements LanguageServerInterface {
fileSystem: this.fs,
}),
disableLanguageServices: false,
disableTaggedHints: false,
disableOrganizeImports: false,
disableWorkspaceSymbol: false,
isInitialized: createInitStatus(),
Expand Down Expand Up @@ -125,6 +126,7 @@ export class TestLanguageService implements LanguageServerInterface {
openFilesOnly: this._workspace.service.getConfigOptions().checkOnlyOpenFiles,
useLibraryCodeForTypes: this._workspace.service.getConfigOptions().useLibraryCodeForTypes,
disableLanguageServices: this._workspace.disableLanguageServices,
disableTaggedHints: this._workspace.disableTaggedHints,
autoImportCompletions: this._workspace.service.getConfigOptions().autoImportCompletions,
functionSignatureDisplay: this._workspace.service.getConfigOptions().functionSignatureDisplay,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ export class TestState {
kinds: [WellKnownWorkspaceKinds.Test],
service: service,
disableLanguageServices: false,
disableTaggedHints: false,
disableOrganizeImports: false,
disableWorkspaceSymbol: false,
isInitialized: createInitStatus(),
Expand Down
2 changes: 2 additions & 0 deletions packages/pyright-internal/src/workspaceFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export interface Workspace {
kinds: string[];
service: AnalyzerService;
disableLanguageServices: boolean;
disableTaggedHints: boolean;
disableOrganizeImports: boolean;
disableWorkspaceSymbol: boolean;
isInitialized: InitStatus;
Expand Down Expand Up @@ -383,6 +384,7 @@ export class WorkspaceFactory {
pythonPathKind,
service: this._createService(name, rootUri, kinds),
disableLanguageServices: false,
disableTaggedHints: false,
disableOrganizeImports: false,
disableWorkspaceSymbol: false,
isInitialized: createInitStatus(),
Expand Down
6 changes: 6 additions & 0 deletions packages/vscode-pyright/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1261,6 +1261,12 @@
"description": "Disables type completion, definitions, and references.",
"scope": "resource"
},
"pyright.disableTaggedHints": {
"type": "boolean",
"default": false,
"description": "Disable hint diagnostics with special hints for grayed-out or strike-through text.",
"scope": "resource"
},
"pyright.disableOrganizeImports": {
"type": "boolean",
"default": false,
Expand Down

0 comments on commit cdcd404

Please sign in to comment.