Skip to content

Commit

Permalink
pull-pylance-with-pyright-1.1.270 (#3920)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
4 people authored Sep 7, 2022
1 parent 2d195c1 commit 95c059f
Show file tree
Hide file tree
Showing 15 changed files with 326 additions and 149 deletions.
80 changes: 40 additions & 40 deletions packages/pyright-internal/package-lock.json

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

8 changes: 4 additions & 4 deletions packages/pyright-internal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
"source-map-support": "^0.5.21",
"tmp": "^0.2.1",
"typescript-char": "^0.0.0",
"vscode-jsonrpc": "8.0.2-next.1",
"vscode-languageserver": "8.0.2-next.4",
"vscode-languageserver-textdocument": "^1.0.5",
"vscode-languageserver-types": "3.17.2-next.2",
"vscode-jsonrpc": "8.1.0-next.1",
"vscode-languageserver": "8.1.0-next.1",
"vscode-languageserver-textdocument": "^1.0.7",
"vscode-languageserver-types": "3.17.2",
"vscode-uri": "^3.0.3"
},
"devDependencies": {
Expand Down
70 changes: 64 additions & 6 deletions packages/pyright-internal/src/analyzer/parseTreeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1639,7 +1639,7 @@ export function getTokenAtLeft(
return tokens.getItemAt(index);
}

function isWhitespace(token: Token) {
export function isWhitespace(token: Token) {
return token.type === TokenType.NewLine || token.type === TokenType.Indent || token.type === TokenType.Dedent;
}

Expand Down Expand Up @@ -2228,23 +2228,28 @@ export function getStringValueRange(token: StringToken) {
export function getFullStatementRange(statementNode: ParseNode, tokenizerOutput: TokenizerOutput): Range {
const range = convertTextRangeToRange(statementNode, tokenizerOutput.lines);

const start = _getStartPositionIfMultipleStatementsAreOnSameLine(range, statementNode.start, tokenizerOutput) ?? {
line: range.start.line,
character: 0,
};

// First, see whether there are other tokens except semicolon or new line on the same line.
const endPosition = _getEndPositionIfMultipleStatementsAreOnSameLine(
const end = _getEndPositionIfMultipleStatementsAreOnSameLine(
range,
TextRange.getEnd(statementNode),
tokenizerOutput
);

if (endPosition) {
return { start: range.start, end: endPosition };
if (end) {
return { start, end };
}

// If not, delete the whole line.
if (range.end.line === tokenizerOutput.lines.count - 1) {
return range;
return { start, end: range.end };
}

return { start: range.start, end: { line: range.end.line + 1, character: 0 } };
return { start, end: { line: range.end.line + 1, character: 0 } };
}

export function isUnannotatedFunction(node: FunctionNode) {
Expand Down Expand Up @@ -2277,6 +2282,55 @@ export function operatorSupportsChaining(op: OperatorType) {
return false;
}

// If the statement is a part of multiple statements on the same line
// and the statement is not the first statement on the line, then it will return
// appropriate start position. otherwise, return undefined.
// ex) a = 1; [|b = 1|]
function _getStartPositionIfMultipleStatementsAreOnSameLine(
range: Range,
tokenPosition: number,
tokenizerOutput: TokenizerOutput
): Position | undefined {
const tokenIndex = tokenizerOutput.tokens.getItemAtPosition(tokenPosition);
if (tokenIndex < 0) {
return undefined;
}

// Find the last token index on the previous line or the first token.
let currentIndex = tokenIndex;
for (; currentIndex > 0; currentIndex--) {
const token = tokenizerOutput.tokens.getItemAt(currentIndex);
const tokenRange = convertTextRangeToRange(token, tokenizerOutput.lines);
if (tokenRange.end.line !== range.start.line) {
break;
}
}

// Find the previous token of the first token of the statement.
for (let index = tokenIndex - 1; index > currentIndex; index--) {
const token = tokenizerOutput.tokens.getItemAt(index);

// Eat up indentation
if (token.type === TokenType.Indent || token.type === TokenType.Dedent) {
continue;
}

// If previous token is new line, use default.
if (token.type === TokenType.NewLine) {
return undefined;
}

// Anything else (ex, semicolon), use statement start as it is.
return range.start;
}

return undefined;
}

// If the statement is a part of multiple statements on the same line
// and the statement is not the last statement on the line, then it will return
// appropriate end position. otherwise, return undefined.
// ex) [|a = 1|]; b = 1
function _getEndPositionIfMultipleStatementsAreOnSameLine(
range: Range,
tokenPosition: number,
Expand All @@ -2287,6 +2341,7 @@ function _getEndPositionIfMultipleStatementsAreOnSameLine(
return undefined;
}

// Find the first token index on the next line or the last token.
let currentIndex = tokenIndex;
for (; currentIndex < tokenizerOutput.tokens.count; currentIndex++) {
const token = tokenizerOutput.tokens.getItemAt(currentIndex);
Expand All @@ -2296,9 +2351,12 @@ function _getEndPositionIfMultipleStatementsAreOnSameLine(
}
}

// Find the next token of the last token of the statement.
let foundStatementEnd = false;
for (let index = tokenIndex; index < currentIndex; index++) {
const token = tokenizerOutput.tokens.getItemAt(index);

// Eat up semicolon or new line.
if (token.type === TokenType.Semicolon || token.type === TokenType.NewLine) {
foundStatementEnd = true;
continue;
Expand Down
30 changes: 23 additions & 7 deletions packages/pyright-internal/src/analyzer/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -634,26 +634,42 @@ export class AnalyzerService {
commandLineOptions.extraPaths
);

this._configFilePath = configFilePath || pyprojectFilePath;

if (commandLineOptions.fileSpecs.length > 0) {
commandLineOptions.fileSpecs.forEach((fileSpec) => {
configOptions.include.push(getFileSpec(this.fs, projectRoot, fileSpec));
});
} else if (!configFilePath) {
// If no config file was found and there are no explicit include
// paths specified, assume the caller wants to include all source
// files under the execution root path.
if (commandLineOptions.executionRoot) {
}

if (commandLineOptions.excludeFileSpecs.length > 0) {
commandLineOptions.excludeFileSpecs.forEach((fileSpec) => {
configOptions.exclude.push(getFileSpec(this.fs, projectRoot, fileSpec));
});
}

if (commandLineOptions.ignoreFileSpecs.length > 0) {
commandLineOptions.ignoreFileSpecs.forEach((fileSpec) => {
configOptions.ignore.push(getFileSpec(this.fs, projectRoot, fileSpec));
});
}

if (!this._configFilePath && commandLineOptions.executionRoot) {
if (commandLineOptions.fileSpecs.length === 0) {
// If no config file was found and there are no explicit include
// paths specified, assume the caller wants to include all source
// files under the execution root path.
configOptions.include.push(getFileSpec(this.fs, commandLineOptions.executionRoot, '.'));
}

if (commandLineOptions.excludeFileSpecs.length === 0) {
// Add a few common excludes to avoid long scan times.
defaultExcludes.forEach((exclude) => {
configOptions.exclude.push(getFileSpec(this.fs, commandLineOptions.executionRoot, exclude));
});
}
}

this._configFilePath = configFilePath || pyprojectFilePath;

// If we found a config file, parse it to compute the effective options.
let configJsonObj: object | undefined;
if (configFilePath) {
Expand Down
9 changes: 9 additions & 0 deletions packages/pyright-internal/src/common/commandLineOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ export class CommandLineOptions {
// are included.
fileSpecs: string[] = [];

// A list of file specs to exclude in the analysis. Can contain
// directories, in which case all "*.py" files within those directories
// are excluded.
excludeFileSpecs: string[] = [];

// A list of file specs whose errors and warnings should be ignored even
// if they are included in the transitive closure of included files.
ignoreFileSpecs: string[] = [];

// Watch for changes in workspace source files.
watchForSourceChanges?: boolean | undefined;

Expand Down
30 changes: 30 additions & 0 deletions packages/pyright-internal/src/common/envVarUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* envVarUtils.ts
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT license.
*
* Utils functions that handles environment variables.
*/

// Expands certain predefined variables supported within VS Code settings.
// Ideally, VS Code would provide an API for doing this expansion, but
// it doesn't. We'll handle the most common variables here as a convenience.
export function expandPathVariables(rootPath: string, value: string): string {
const regexp = /\$\{(.*?)\}/g;
return value.replace(regexp, (match: string, name: string) => {
const trimmedName = name.trim();
if (trimmedName === 'workspaceFolder') {
return rootPath;
}
if (trimmedName === 'env:HOME' && process.env.HOME !== undefined) {
return process.env.HOME;
}
if (trimmedName === 'env:USERNAME' && process.env.USERNAME !== undefined) {
return process.env.USERNAME;
}
if (trimmedName === 'env:VIRTUAL_ENV' && process.env.VIRTUAL_ENV !== undefined) {
return process.env.VIRTUAL_ENV;
}
return match;
});
}
2 changes: 1 addition & 1 deletion packages/pyright-internal/src/common/realFileSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ class RealFileSystem implements FileSystem {
return realPath.substr(0, rootLength).toLowerCase() + realPath.substr(rootLength);
} catch (e: any) {
// Return as it is, if anything failed.
this._console.error(`Failed to get real file system casing for ${path}: ${e}`);
this._console.log(`Failed to get real file system casing for ${path}: ${e}`);

return path;
}
Expand Down
Loading

0 comments on commit 95c059f

Please sign in to comment.