8000 pull-pylance-with-pyright-1.1.266 (#3810) · codean-io/scip-python@888eb24 · GitHub
[go: up one dir, main page]

Skip to content

Commit 888eb24

Browse files
authored
pull-pylance-with-pyright-1.1.266 (#3810)
Co-authored-by: Co-authored-by: Bill Schnurr bschnurr@microsoft.com Co-authored-by: HeeJae Chang hechang@microsoft.com Co-authored-by: Erik De Bonte erikd@microsoft.com
1 parent 162406b commit 888eb24

15 files changed

+165
-145
lines changed

packages/pyright-internal/src/analyzer/program.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1888,7 +1888,7 @@ export class Program {
18881888
isDefaultWorkspace: boolean,
18891889
allowModuleRename: boolean,
18901890
token: CancellationToken
1891-
): Range | undefined {
1891+
): { range: Range; declarations: Declaration[] } | undefined {
18921892
return this._runEvaluatorWithCancellationToken(token, () => {
18931893
const sourceFileInfo = this._getSourceFileInfoFromPath(filePath);
18941894
if (!sourceFileInfo) {
@@ -1921,7 +1921,10 @@ export class Program {
19211921

19221922
// Return the range of the symbol.
19231923
const parseResult = sourceFileInfo.sourceFile.getParseResults()!;
1924-
return convertTextRangeToRange(referencesResult.nodeAtOffset, parseResult.tokenizerOutput.lines);
1924+
return {
1925+
range: convertTextRangeToRange(referencesResult.nodeAtOffset, parseResult.tokenizerOutput.lines),
1926+
declarations: referencesResult.declarations,
1927+
};
19251928
});
19261929
}
19271930

packages/pyright-internal/src/analyzer/service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ export class AnalyzerService {
444444
isDefaultWorkspace: boolean,
445445
allowModuleRename: boolean,
446446
token: CancellationToken
447-
): Range | undefined {
447+
) {
448448
return this._program.canRenameSymbolAtPosition(
449449
filePath,
450450
position,

packages/pyright-internal/src/languageServerBase.ts

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -148,18 +148,23 @@ export enum WellKnownWorkspaceKinds {
148148
Test = 'test',
149149
}
150150

151+
// path and uri will point to a workspace itself. It could be a folder
152+
// if the workspace represents a folder. it could be '' if it is the default workspace.
153+
// But it also could be a file if it is a virtual workspace.
154+
// rootPath will always point to the folder that contains the workspace.
151155
export interface WorkspaceServiceInstance {
152156
workspaceName: string;
153157
rootPath: string;
154-
rootUri: string;
155-
kind: string;
158+
path: string;
159+
uri: string;
160+
kinds: string[];
156161
serviceInstance: AnalyzerService;
157162
disableLanguageServices: boolean;
158163
disableOrganizeImports: boolean;
159164
disableWorkspaceSymbol: boolean;
160165
isInitialized: Deferred<boolean>;
161166
searchPathsToWatch: string[];
162-
owns?(filePath: string): boolean;
167+
owns(filePath: string): boolean;
163168
}
164169

165170
export interface MessageAction {
@@ -574,10 +579,13 @@ export abstract class LanguageServerBase implements LanguageServerInterface {
574579
if (params.workspaceFolders) {
575580
params.workspaceFolders.forEach((folder) => {
576581
const path = this._uriParser.decodeTextDocumentUri(folder.uri);
577-
this._workspaceMap.set(path, this.createWorkspaceServiceInstance(folder, path));
582+
this._workspaceMap.set(path, this.createWorkspaceServiceInstance(folder, path, path));
578583
});
579584
} else if (params.rootPath) {
580-
this._workspaceMap.set(params.rootPath, this.createWorkspaceServiceInstance(undefined, params.rootPath));
585+
this._workspaceMap.set(
586+
params.rootPath,
587+
this.createWorkspaceServiceInstance(undefined, params.rootPath, params.rootPath)
588+
);
581589
}
582590

583591
const result: InitializeResult = {
@@ -629,7 +637,7 @@ export abstract class LanguageServerBase implements LanguageServerInterface {
629637

630638
event.added.forEach(async (workspace) => {
631639
const rootPath = this._uriParser.decodeTextDocumentUri(workspace.uri);
632-
const newWorkspace = this.createWorkspaceServiceInstance(workspace, rootPath);
640+
const newWorkspace = this.createWorkspaceServiceInstance(workspace, rootPath, rootPath);
633641
this._workspaceMap.set(rootPath, newWorkspace);
634642
await this.updateSettingsForWorkspace(newWorkspace);
635643
});
@@ -1014,7 +1022,6 @@ export abstract class LanguageServerBase implements LanguageServerInterface {
10141022
workspace,
10151023
filePath,
10161024
position,
1017-
workspace.rootPath,
10181025
this.getCompletionOptions(workspace, params),
10191026
token
10201027
);
@@ -1052,15 +1059,21 @@ export abstract class LanguageServerBase implements LanguageServerInterface {
10521059
return null;
10531060
}
10541061

1055-
const range = workspace.serviceInstance.canRenameSymbolAtPosition(
1062+
const result = workspace.serviceInstance.canRenameSymbolAtPosition(
10561063
filePath,
10571064
position,
1058-
workspace.rootPath === '',
1065+
workspace.path === '',
10591066
this.allowModuleRename,
10601067
token
10611068
);
10621069

1063-
return range ?? null;
1070+
// We only allow renaming symbol defined in the files this workspace owns.
1071+
// This is to make sure we don't rename files across workspaces in multiple workspaces context.
1072+
if (result && result.declarations.some((d) => d.path && !workspace.owns(d.path))) {
1073+
return null;
1074+
}
1075+
1076+
return result?.range ?? null;
10641077
}
10651078

10661079
protected async onRenameRequest(
@@ -1078,7 +1091,7 @@ export abstract class LanguageServerBase implements LanguageServerInterface {
10781091
filePath,
10791092
position,
10801093
params.newName,
1081-
workspace.rootPath === '',
1094+
workspace.path === '',
10821095
this.allowModuleRename,
10831096
token
10841097
);
@@ -1286,14 +1299,13 @@ export abstract class LanguageServerBase implements LanguageServerInterface {
12861299
workspace: WorkspaceServiceInstance,
12871300
filePath: string,
12881301
position: Position,
1289-
workspacePath: string,
12901302
options: CompletionOptions,
12911303
token: CancellationToken
12921304
): Promise<CompletionResultsList | undefined> {
12931305
return workspace.serviceInstance.getCompletionsForPosition(
12941306
filePath,
12951307
position,
1296-
workspacePath,
1308+
workspace.path,
12971309
options,
12981310
undefined,
12991311
token
@@ -1323,9 +1335,10 @@ export abstract class LanguageServerBase implements LanguageServerInterface {
13231335
}
13241336

13251337
protected createWorkspaceServiceInstance(
1326-
workspace: WorkspaceFolder | undefined,
1338+
workspaceFolder: WorkspaceFolder | undefined,
13271339
rootPath: string,
1328-
kind: string = WellKnownWorkspaceKinds.Regular,
1340+
path: string,
1341+
kinds: string[] = [WellKnownWorkspaceKinds.Regular],
13291342
owns?: (filePath: string) => boolean,
13301343
services?: WorkspaceServices
13311344
): WorkspaceServiceInstance {
@@ -1336,19 +1349,24 @@ export abstract class LanguageServerBase implements LanguageServerInterface {
13361349
const multiWorkspaceBackOffTime = 10 * 60 * 1000;
13371350

13381351
const libraryReanalysisTimeProvider =
1339-
kind === WellKnownWorkspaceKinds.Regular
1352+
kinds.length === 1 && kinds[0] === WellKnownWorkspaceKinds.Regular
13401353
? () =>
1341-
this._workspaceMap.hasMultipleWorkspaces(kind) ? multiWorkspaceBackOffTime : defaultBackOffTime
1354+
this._workspaceMap.hasMultipleWorkspaces(kinds[0])
1355+
? multiWorkspaceBackOffTime
1356+
: defaultBackOffTime
13421357
: () => defaultBackOffTime;
13431358

1344-
const rootUri = workspace?.uri ?? '';
1359+
const rootUri = workspaceFolder?.uri ?? '';
1360+
owns = owns ?? ((f) => f.startsWith(rootPath));
1361+
13451362
return {
1346-
workspaceName: workspace?.name ?? '',
1363+
workspaceName: workspaceFolder?.name ?? '',
13471364
rootPath,
1348-
rootUri,
1349-
kind,
1365+
path,
1366+
uri: rootUri,
1367+
kinds,
13501368
serviceInstance: this.createAnalyzerService(
1351-
workspace?.name ?? rootPath,
1369+
workspaceFolder?.name ?? path,
13521370
services,
13531371
libraryReanalysisTimeProvider
13541372
),

packages/pyright-internal/src/languageService/analyzerServiceExecutor.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,16 @@ export class AnalyzerServiceExecutor {
5353
const tempWorkspace: WorkspaceServiceInstance = {
5454
workspaceName: `temp workspace for cloned service`,
5555
rootPath: workspace.rootPath,
56-
rootUri: workspace.rootUri,
57-
kind: WellKnownWorkspaceKinds.Cloned,
56+
path: workspace.path,
57+
uri: workspace.uri,
58+
kinds: [...workspace.kinds, WellKnownWorkspaceKinds.Cloned],
5859
serviceInstance: workspace.serviceInstance.clone('cloned service', backgroundAnalysis, fileSystem),
5960
disableLanguageServices: true,
6061
disableOrganizeImports: true,
6162
disableWorkspaceSymbol: true,
6263
isInitialized: createDeferred<boolean>(),
6364
searchPathsToWatch: [],
65+
owns: workspace.owns,
6466
};
6567

6668
const serverSettings = await ls.getSettings(workspace);

packages/pyright-internal/src/languageService/codeActionProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export class CodeActionProvider {
4545
Command.create(
4646
Localizer.CodeAction.createTypeStub(),
4747
Commands.createTypeStub,
48-
workspace.rootPath,
48+
workspace.path,
4949
action.moduleName,
5050
filePath
5151
),

packages/pyright-internal/src/languageService/completionProvider.ts

Lines changed: 9 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,6 @@ import { Symbol, SymbolTable } from '../analyzer/symbol';
4141
import * as SymbolNameUtils from '../analyzer/symbolNameUtils';
4242
import { getLastTypedDeclaredForSymbol } from '../analyzer/symbolUtils';
4343
import { getTypedDictMembersForClass } from '../analyzer/typedDicts';
44-
import {
45-
getClassDocString,
46-
getModuleDocString,
47-
getPropertyDocStringInherited,
48-
getVariableDocString,
49-
} from '../analyzer/typeDocStringUtils';
5044
import { CallSignatureInfo, TypeEvaluator } from '../analyzer/typeEvaluatorTypes';
5145
import { printLiteralValue } from '../analyzer/typePrinter';
5246
import {
@@ -115,12 +109,7 @@ import { StringToken, StringTokenFlags, Token, TokenType } from '../parser/token
115109
import { AbbreviationInfo, AutoImporter, AutoImportResult, ImportFormat, ModuleSymbolMap } from './autoImporter';
116110
import { DocumentSymbolCollector } from './documentSymbolCollector';
117111
import { IndexResults } from './documentSymbolProvider';
118-
import {
119-
getAutoImportText,
120-
getFunctionDocStringFromType,
121-
getOverloadedFunctionDocStringsFromType,
122-
getOverloadedFunctionTooltip,
123-
} from './tooltipUtils';
112+
import { getAutoImportText, getDocumentationPartsForTypeAndDecl, getOverloadedFunctionTooltip } from './tooltipUtils';
124113

125114
namespace Keywords {
126115
const base: string[] = [
@@ -2632,52 +2621,14 @@ export class CompletionProvider {
26322621
}
26332622
}
26342623

2635-
if (
2636-
primaryDecl.type === DeclarationType.Variable &&
2637-
primaryDecl.typeAliasName &&
2638-
primaryDecl.docString
2639-
) {
2640-
documentation = primaryDecl.docString;
2641-
} else if (isModule(type)) {
2642-
documentation = getModuleDocString(type, primaryDecl, this._sourceMapper);
2643-
} else if (isInstantiableClass(type)) {
2644-
documentation = getClassDocString(type, primaryDecl, this._sourceMapper);
2645-
} else if (isFunction(type)) {
2646-
const functionType = detail.boundObjectOrClass
2647-
? this._evaluator.bindFunctionToClassOrObject(detail.boundObjectOrClass, type)
2648-
: type;
2649-
if (functionType && isFunction(functionType)) {
2650-
documentation = getFunctionDocStringFromType(
2651-
functionType,
2652-
this._sourceMapper,
2653-
this._evaluator
2654-
);
2655-
}
2656-
} else if (isOverloadedFunction(type)) {
2657-
const functionType = detail.boundObjectOrClass
2658-
? this._evaluator.bindFunctionToClassOrObject(detail.boundObjectOrClass, type)
2659-
: type;
2660-
if (functionType && isOverloadedFunction(functionType)) {
2661-
documentation = getOverloadedFunctionDocStringsFromType(
2662-
functionType,
2663-
this._sourceMapper,
2664-
this._evaluator
2665-
).find((doc) => doc);
2666-
}
2667-
} else if (primaryDecl?.type === DeclarationType.Function) {
2668-
// @property functions
2669-
documentation = getPropertyDocStringInherited(
2670-
primaryDecl,
2671-
this._sourceMapper,
2672-
this._evaluator
2673-
);
2674-
} else if (primaryDecl?.type === DeclarationType.Variable) {
2675-
const decl = (symbol
2676-
.getDeclarations()
2677-
.find((d) => d.type === DeclarationType.Variable && !!d.docString) ??
2678-
primaryDecl) as VariableDeclaration;
2679-
documentation = getVariableDocString(decl, this._sourceMapper);
2680-
}
2624+
documentation = getDocumentationPartsForTypeAndDecl(
2625+
this._sourceMapper,
2626+
type,
2627+
primaryDecl,
2628+
this._evaluator,
2629+
symbol,
2630+
detail.boundObjectOrClass
2631+
);
26812632

26822633
if (this._options.format === MarkupKind.Markdown) {
26832634
let markdownString = '```python\n' + typeDetail + '\n```\n';

packages/pyright-internal/src/languageService/hoverProvider.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -450,16 +450,13 @@ export class HoverProvider {
450450
resolvedDecl: Declaration | undefined,
451451
evaluator: TypeEvaluator
452452
): boolean {
453-
const docStrings = getDocumentationPartsForTypeAndDecl(sourceMapper, type, resolvedDecl, evaluator);
454-
let addedDoc = false;
455-
for (const docString of docStrings) {
456-
if (docString) {
457-
addedDoc = true;
458-
this._addDocumentationResultsPart(format, parts, docString);
459-
}
453+
const docString = getDocumentationPartsForTypeAndDecl(sourceMapper, type, resolvedDecl, evaluator);
454+
if (docString) {
455+
this._addDocumentationResultsPart(format, parts, docString);
456+
return true;
460457
}
461458

462-
return addedDoc;
459+
return false;
463460
}
464461

465462
private static _addDocumentationResultsPart(format: MarkupKind, parts: HoverTextPart[], docString?: string) {

packages/pyright-internal/src/languageService/signatureHelpProvider.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,9 @@ export class SignatureHelpProvider {
218218
continue;
219219
}
220220

221-
const parts = getDocumentationPartsForTypeAndDecl(sourceMapper, type, resolveDecl, evaluator);
222-
if (parts.length > 0) {
223-
return parts.join('\n\n');
221+
const part = getDocumentationPartsForTypeAndDecl(sourceMapper, type, resolveDecl, evaluator);
222+
if (part) {
223+
return part;
224224
}
225225
}
226226

0 commit comments

Comments
 (0)
0