@zzzen/pyright-internal 1.2.0-dev.20230409 → 1.2.0-dev.20230423

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (151) hide show
  1. package/dist/analyzer/binder.js +1 -3
  2. package/dist/analyzer/binder.js.map +1 -1
  3. package/dist/analyzer/codeFlowEngine.js +22 -0
  4. package/dist/analyzer/codeFlowEngine.js.map +1 -1
  5. package/dist/analyzer/commentUtils.d.ts +1 -1
  6. package/dist/analyzer/commentUtils.js +18 -3
  7. package/dist/analyzer/commentUtils.js.map +1 -1
  8. package/dist/analyzer/constructorTransform.js +112 -49
  9. package/dist/analyzer/constructorTransform.js.map +1 -1
  10. package/dist/analyzer/declarationUtils.js +2 -1
  11. package/dist/analyzer/declarationUtils.js.map +1 -1
  12. package/dist/analyzer/docStringConversion.js +2 -3
  13. package/dist/analyzer/docStringConversion.js.map +1 -1
  14. package/dist/analyzer/enums.d.ts +1 -1
  15. package/dist/analyzer/enums.js +114 -55
  16. package/dist/analyzer/enums.js.map +1 -1
  17. package/dist/analyzer/parseTreeUtils.js +9 -4
  18. package/dist/analyzer/parseTreeUtils.js.map +1 -1
  19. package/dist/analyzer/patternMatching.d.ts +6 -2
  20. package/dist/analyzer/patternMatching.js +109 -2
  21. package/dist/analyzer/patternMatching.js.map +1 -1
  22. package/dist/analyzer/program.d.ts +9 -7
  23. package/dist/analyzer/program.js +106 -46
  24. package/dist/analyzer/program.js.map +1 -1
  25. package/dist/analyzer/service.d.ts +2 -2
  26. package/dist/analyzer/service.js +6 -6
  27. package/dist/analyzer/service.js.map +1 -1
  28. package/dist/analyzer/sourceFile.d.ts +1 -3
  29. package/dist/analyzer/sourceFile.js +1 -9
  30. package/dist/analyzer/sourceFile.js.map +1 -1
  31. package/dist/analyzer/sourceMapper.js +1 -1
  32. package/dist/analyzer/sourceMapper.js.map +1 -1
  33. package/dist/analyzer/typeEvaluator.js +147 -38
  34. package/dist/analyzer/typeEvaluator.js.map +1 -1
  35. package/dist/analyzer/typeEvaluatorTypes.d.ts +4 -3
  36. package/dist/analyzer/typeEvaluatorTypes.js +0 -2
  37. package/dist/analyzer/typeEvaluatorTypes.js.map +1 -1
  38. package/dist/analyzer/typeGuards.d.ts +2 -0
  39. package/dist/analyzer/typeGuards.js +172 -175
  40. package/dist/analyzer/typeGuards.js.map +1 -1
  41. package/dist/analyzer/typePrinter.d.ts +1 -1
  42. package/dist/analyzer/typePrinter.js +21 -18
  43. package/dist/analyzer/typePrinter.js.map +1 -1
  44. package/dist/analyzer/typeUtils.js +13 -13
  45. package/dist/analyzer/typeUtils.js.map +1 -1
  46. package/dist/analyzer/types.d.ts +2 -1
  47. package/dist/analyzer/types.js +20 -4
  48. package/dist/analyzer/types.js.map +1 -1
  49. package/dist/common/configOptions.d.ts +1 -1
  50. package/dist/common/configOptions.js +3 -3
  51. package/dist/common/configOptions.js.map +1 -1
  52. package/dist/common/extensibility.d.ts +7 -4
  53. package/dist/common/extensibility.js.map +1 -1
  54. package/dist/common/fullAccessHost.d.ts +2 -1
  55. package/dist/common/fullAccessHost.js +11 -1
  56. package/dist/common/fullAccessHost.js.map +1 -1
  57. package/dist/common/host.d.ts +3 -2
  58. package/dist/common/host.js +1 -1
  59. package/dist/common/host.js.map +1 -1
  60. package/dist/languageServerBase.js +3 -2
  61. package/dist/languageServerBase.js.map +1 -1
  62. package/dist/languageService/autoImporter.d.ts +1 -1
  63. package/dist/languageService/autoImporter.js +2 -2
  64. package/dist/languageService/autoImporter.js.map +1 -1
  65. package/dist/languageService/callHierarchyProvider.js +17 -4
  66. package/dist/languageService/callHierarchyProvider.js.map +1 -1
  67. package/dist/languageService/completionProvider.js +259 -93
  68. package/dist/languageService/completionProvider.js.map +1 -1
  69. package/dist/languageService/completionProviderUtils.js +11 -3
  70. package/dist/languageService/completionProviderUtils.js.map +1 -1
  71. package/dist/languageService/documentSymbolCollector.js +1 -1
  72. package/dist/languageService/documentSymbolCollector.js.map +1 -1
  73. package/dist/languageService/hoverProvider.d.ts +28 -28
  74. package/dist/languageService/hoverProvider.js +151 -118
  75. package/dist/languageService/hoverProvider.js.map +1 -1
  76. package/dist/languageService/importAdder.d.ts +13 -2
  77. package/dist/languageService/importAdder.js +73 -26
  78. package/dist/languageService/importAdder.js.map +1 -1
  79. package/dist/languageService/indentationUtils.js +9 -4
  80. package/dist/languageService/indentationUtils.js.map +1 -1
  81. package/dist/languageService/insertionPointUtils.js +2 -2
  82. package/dist/languageService/insertionPointUtils.js.map +1 -1
  83. package/dist/localization/localize.d.ts +2 -0
  84. package/dist/localization/localize.js +2 -0
  85. package/dist/localization/localize.js.map +1 -1
  86. package/dist/localization/package.nls.en-us.json +2 -0
  87. package/dist/tests/checker.test.js +1 -1
  88. package/dist/tests/completions.test.js +412 -0
  89. package/dist/tests/completions.test.js.map +1 -1
  90. package/dist/tests/docStringConversion.test.js +11 -1
  91. package/dist/tests/docStringConversion.test.js.map +1 -1
  92. package/dist/tests/fourslash/completions.inherited.function.docFromStub.fourslash.js +1 -1
  93. package/dist/tests/fourslash/completions.inherited.function.docFromStub.fourslash.js.map +1 -1
  94. package/dist/tests/fourslash/completions.override2.fourslash.js +16 -1
  95. package/dist/tests/fourslash/completions.override2.fourslash.js.map +1 -1
  96. package/dist/tests/fourslash/completions.variableDocStrings.fourslash.js +1 -1
  97. package/dist/tests/fourslash/completions.variableDocStrings.fourslash.js.map +1 -1
  98. package/dist/tests/fourslash/fourslash.d.ts +11 -1
  99. package/dist/tests/fourslash/hover.docstring.overloads.fourslash.js +2 -2
  100. package/dist/tests/fourslash/hover.docstring.overloads.fourslash.js.map +1 -1
  101. package/dist/tests/fourslash/hover.variable.docString.fourslash.js +1 -1
  102. package/dist/tests/fourslash/hover.variable.docString.fourslash.js.map +1 -1
  103. package/dist/tests/fourslash/showcallhierarchy.incomingCalls.aliasedFunction.fourslash.js +5 -6
  104. package/dist/tests/fourslash/showcallhierarchy.incomingCalls.aliasedFunction.fourslash.js.map +1 -1
  105. package/dist/tests/fourslash/showcallhierarchy.incomingCalls.aliasedVariable.fourslash.js +1 -4
  106. package/dist/tests/fourslash/showcallhierarchy.incomingCalls.aliasedVariable.fourslash.js.map +1 -1
  107. package/dist/tests/fourslash/showcallhierarchy.incomingCalls.function.fourslash.js +10 -9
  108. package/dist/tests/fourslash/showcallhierarchy.incomingCalls.function.fourslash.js.map +1 -1
  109. package/dist/tests/fourslash/showcallhierarchy.outgoingCalls.aliasedFunction1.fourslash.d.ts +1 -0
  110. package/dist/tests/fourslash/showcallhierarchy.outgoingCalls.aliasedFunction1.fourslash.js +54 -0
  111. package/dist/tests/fourslash/showcallhierarchy.outgoingCalls.aliasedFunction1.fourslash.js.map +1 -0
  112. package/dist/tests/fourslash/showcallhierarchy.outgoingCalls.aliasedFunction2.fourslash.d.ts +1 -0
  113. package/dist/tests/fourslash/showcallhierarchy.outgoingCalls.aliasedFunction2.fourslash.js +46 -0
  114. package/dist/tests/fourslash/showcallhierarchy.outgoingCalls.aliasedFunction2.fourslash.js.map +1 -0
  115. package/dist/tests/fourslash/showcallhierarchy.outgoingCalls.function.fourslash.d.ts +1 -0
  116. package/dist/tests/fourslash/showcallhierarchy.outgoingCalls.function.fourslash.js +39 -0
  117. package/dist/tests/fourslash/showcallhierarchy.outgoingCalls.function.fourslash.js.map +1 -0
  118. package/dist/tests/harness/fourslash/fourSlashParser.js +1 -1
  119. package/dist/tests/harness/fourslash/fourSlashParser.js.map +1 -1
  120. package/dist/tests/harness/fourslash/testState.d.ts +6 -1
  121. package/dist/tests/harness/fourslash/testState.js +38 -6
  122. package/dist/tests/harness/fourslash/testState.js.map +1 -1
  123. package/dist/tests/harness/testHost.js +1 -1
  124. package/dist/tests/harness/testHost.js.map +1 -1
  125. package/dist/tests/harness/vfs/factory.js +2 -2
  126. package/dist/tests/harness/vfs/factory.js.map +1 -1
  127. package/dist/tests/harness/vfs/filesystem.js +26 -25
  128. package/dist/tests/harness/vfs/filesystem.js.map +1 -1
  129. package/dist/tests/hoverProvider.test.js +6 -0
  130. package/dist/tests/hoverProvider.test.js.map +1 -1
  131. package/dist/tests/importAdder.test.js +69 -1
  132. package/dist/tests/importAdder.test.js.map +1 -1
  133. package/dist/tests/indentationUtils.ptvs.test.js +2 -2
  134. package/dist/tests/indentationUtils.ptvs.test.js.map +1 -1
  135. package/dist/tests/indentationUtils.test.js +18 -0
  136. package/dist/tests/indentationUtils.test.js.map +1 -1
  137. package/dist/tests/moveSymbol.insertion.test.js +25 -4
  138. package/dist/tests/moveSymbol.insertion.test.js.map +1 -1
  139. package/dist/tests/typeEvaluator1.test.js +8 -0
  140. package/dist/tests/typeEvaluator1.test.js.map +1 -1
  141. package/dist/tests/typeEvaluator2.test.js +4 -0
  142. package/dist/tests/typeEvaluator2.test.js.map +1 -1
  143. package/dist/tests/typeEvaluator3.test.js +10 -0
  144. package/dist/tests/typeEvaluator3.test.js.map +1 -1
  145. package/dist/tests/typeEvaluator4.test.js +5 -1
  146. package/dist/tests/typeEvaluator4.test.js.map +1 -1
  147. package/dist/tests/typePrinter.test.js +5 -0
  148. package/dist/tests/typePrinter.test.js.map +1 -1
  149. package/dist/workspaceFactory.js +1 -1
  150. package/dist/workspaceFactory.js.map +1 -1
  151. package/package.json +1 -1
@@ -6,13 +6,13 @@ import { ConsoleInterface } from '../common/console';
6
6
  import { Diagnostic } from '../common/diagnostic';
7
7
  import { FileDiagnostics } from '../common/diagnosticSink';
8
8
  import { FileEditActions, TextEditAction } from '../common/editAction';
9
+ import { ProgramView } from '../common/extensibility';
9
10
  import { LogTracker } from '../common/logTracker';
10
11
  import { DocumentRange, Position, Range } from '../common/textRange';
11
12
  import { AutoImportOptions, AutoImportResult, ImportFormat } from '../languageService/autoImporter';
12
13
  import { AbbreviationMap, CompletionOptions, CompletionResultsList } from '../languageService/completionProvider';
13
14
  import { DefinitionFilter } from '../languageService/definitionProvider';
14
15
  import { IndexOptions, IndexResults, WorkspaceSymbolCallback } from '../languageService/documentSymbolProvider';
15
- import { HoverResults } from '../languageService/hoverProvider';
16
16
  import { ReferenceCallback } from '../languageService/referencesProvider';
17
17
  import { SignatureHelpResults } from '../languageService/signatureHelpProvider';
18
18
  import { ParseResults } from '../parser/parser';
@@ -74,14 +74,14 @@ export declare class Program {
74
74
  private static _nextId;
75
75
  constructor(initialImportResolver: ImportResolver, initialConfigOptions: ConfigOptions, console?: ConsoleInterface, logTracker?: LogTracker, _disableChecker?: boolean | undefined, cacheManager?: CacheManager);
76
76
  dispose(): void;
77
- get evaluator(): TypeEvaluator | undefined;
78
- get console(): ConsoleInterface;
79
77
  get id(): number;
80
- setConfigOptions(configOptions: ConfigOptions): void;
78
+ get console(): ConsoleInterface;
81
79
  get rootPath(): string;
82
- getConfigOptions(): ConfigOptions;
80
+ get evaluator(): TypeEvaluator | undefined;
81
+ get configOptions(): ConfigOptions;
82
+ get importResolver(): ImportResolver;
83
+ setConfigOptions(configOptions: ConfigOptions): void;
83
84
  setImportResolver(importResolver: ImportResolver): void;
84
- getImportResolver(): ImportResolver;
85
85
  setTrackedFiles(filePaths: string[]): FileDiagnostics[];
86
86
  setPreCheckCallback(preCheckCallback: PreCheckCallback): void;
87
87
  setAllowedThirdPartyImports(importNames: string[]): void;
@@ -110,6 +110,9 @@ export declare class Program {
110
110
  analyze(maxTime?: MaxAnalysisTime, token?: CancellationToken): boolean;
111
111
  analyzeFile(filePath: string, token?: CancellationToken): boolean;
112
112
  indexWorkspace(callback: (path: string, results: IndexResults) => void, token: CancellationToken): number;
113
+ run<T>(callback: (p: ProgramView) => T, token: CancellationToken): T;
114
+ getSourceMapper(filePath: string, token: CancellationToken, mapCompiled?: boolean, preferStubs?: boolean): SourceMapper;
115
+ getParseResults(filePath: string): ParseResults | undefined;
113
116
  printDetailedAnalysisTimes(): void;
114
117
  printDependencies(projectRootDir: string, verbose: boolean): void;
115
118
  writeTypeStub(targetImportPath: string, targetIsSingleFile: boolean, stubPath: string, token: CancellationToken): void;
@@ -144,7 +147,6 @@ export declare class Program {
144
147
  getFileIndex(filePath: string, options: IndexOptions, token: CancellationToken): IndexResults | undefined;
145
148
  addSymbolsForDocument(filePath: string, symbolList: DocumentSymbol[], token: CancellationToken): void;
146
149
  reportSymbolsForWorkspace(query: string, reporter: WorkspaceSymbolCallback, token: CancellationToken): void;
147
- getHoverForPosition(filePath: string, position: Position, format: MarkupKind, token: CancellationToken): HoverResults | undefined;
148
150
  getDocumentHighlight(filePath: string, position: Position, token: CancellationToken): DocumentHighlight[] | undefined;
149
151
  getSignatureHelpForPosition(filePath: string, position: Position, format: MarkupKind, token: CancellationToken): SignatureHelpResults | undefined;
150
152
  getCompletionsForPosition(filePath: string, position: Position, workspacePath: string, options: CompletionOptions, nameMap: AbbreviationMap | undefined, libraryMap: Map<string, IndexResults> | undefined, token: CancellationToken): Promise<CompletionResultsList | undefined>;
@@ -59,6 +59,7 @@ const renameModuleProvider_1 = require("../languageService/renameModuleProvider"
59
59
  const AnalyzerNodeInfo = __importStar(require("./analyzerNodeInfo"));
60
60
  const cacheManager_1 = require("./cacheManager");
61
61
  const circularDependency_1 = require("./circularDependency");
62
+ const declaration_1 = require("./declaration");
62
63
  const parseTreeUtils_1 = require("./parseTreeUtils");
63
64
  const scopeUtils_1 = require("./scopeUtils");
64
65
  const sourceFile_1 = require("./sourceFile");
@@ -150,14 +151,23 @@ class Program {
150
151
  dispose() {
151
152
  this._cacheManager.unregisterCacheOwner(this);
152
153
  }
153
- get evaluator() {
154
- return this._evaluator;
154
+ get id() {
155
+ return this._id;
155
156
  }
156
157
  get console() {
157
158
  return this._console;
158
159
  }
159
- get id() {
160
- return this._id;
160
+ get rootPath() {
161
+ return this._configOptions.projectRoot;
162
+ }
163
+ get evaluator() {
164
+ return this._evaluator;
165
+ }
166
+ get configOptions() {
167
+ return this._configOptions;
168
+ }
169
+ get importResolver() {
170
+ return this._importResolver;
161
171
  }
162
172
  setConfigOptions(configOptions) {
163
173
  this._configOptions = configOptions;
@@ -165,12 +175,6 @@ class Program {
165
175
  // Create a new evaluator with the updated config options.
166
176
  this._createNewEvaluator();
167
177
  }
168
- get rootPath() {
169
- return this._configOptions.projectRoot;
170
- }
171
- getConfigOptions() {
172
- return this._configOptions;
173
- }
174
178
  setImportResolver(importResolver) {
175
179
  this._importResolver = importResolver;
176
180
  // Create a new evaluator with the updated import resolver.
@@ -178,9 +182,6 @@ class Program {
178
182
  // older import resolver when resolving imports after parsing.
179
183
  this._createNewEvaluator();
180
184
  }
181
- getImportResolver() {
182
- return this._importResolver;
183
- }
184
185
  // Sets the list of tracked files that make up the program.
185
186
  setTrackedFiles(filePaths) {
186
187
  if (this._sourceFileList.length > 0) {
@@ -539,6 +540,22 @@ class Program {
539
540
  }
540
541
  }
541
542
  }
543
+ // This will allow the callback to execute a type evaluator with an associated
544
+ // cancellation token and provide a read-only program.
545
+ run(callback, token) {
546
+ var _a;
547
+ const evaluator = (_a = this._evaluator) !== null && _a !== void 0 ? _a : this._createNewEvaluator();
548
+ return evaluator.runWithCancellationToken(token, () => callback(this));
549
+ }
550
+ getSourceMapper(filePath, token, mapCompiled, preferStubs) {
551
+ const sourceFileInfo = this.getSourceFileInfo(filePath);
552
+ const execEnv = this._configOptions.findExecEnvironment(filePath);
553
+ return this._createSourceMapper(execEnv, token, sourceFileInfo, mapCompiled, preferStubs);
554
+ }
555
+ getParseResults(filePath) {
556
+ var _a;
557
+ return (_a = this.getBoundSourceFile(filePath)) === null || _a === void 0 ? void 0 : _a.getParseResults();
558
+ }
542
559
  // Prints a detailed list of files that have been checked and the times associated
543
560
  // with each of them, sorted greatest to least.
544
561
  printDetailedAnalysisTimes() {
@@ -654,8 +671,8 @@ class Program {
654
671
  if (configOptions.diagnosticRuleSet.omitConditionalConstraint) {
655
672
  flags |= 64 /* OmitConditionalConstraint */;
656
673
  }
657
- if (configOptions.diagnosticRuleSet.omitTypeArgsIfAny) {
658
- flags |= 2 /* OmitTypeArgumentsIfAny */;
674
+ if (configOptions.diagnosticRuleSet.omitTypeArgsIfUnknown) {
675
+ flags |= 2 /* OmitTypeArgumentsIfUnknown */;
659
676
  }
660
677
  if (configOptions.diagnosticRuleSet.omitUnannotatedParamType) {
661
678
  flags |= 4 /* OmitUnannotatedParamType */;
@@ -738,8 +755,10 @@ class Program {
738
755
  if (!force && (!this._isFileNeeded(fileToParse) || !fileToParse.sourceFile.isParseRequired())) {
739
756
  return;
740
757
  }
741
- // sourceFile.parse should never be called directly. Otherwise, whole dependency graph maintained
742
- // by program will be broken. Use _parseFile instead.
758
+ // SourceFile.parse should only be called here in the program, as calling it
759
+ // elsewhere could break the entire dependency graph maintained by the program.
760
+ // Other parts of the program should use _parseFile to create ParseResults from
761
+ // the sourceFile. For standalone parseResults, use parseFile or the Parser directly.
743
762
  if (fileToParse.sourceFile.parse(this._configOptions, this._importResolver, content)) {
744
763
  this._parsedFileCount++;
745
764
  this._updateSourceFileImports(fileToParse, this._configOptions);
@@ -793,7 +812,7 @@ class Program {
793
812
  let implicitImport = implicitImports.pop();
794
813
  while (implicitImport) {
795
814
  // Bind this file, but don't recurse into its imports.
796
- this._bindFile(implicitImport, undefined, undefined, /*isImplicitImport*/ true);
815
+ this._bindFile(implicitImport, undefined, undefined, /* isImplicitImport */ true);
797
816
  implicitImport = implicitImports.pop();
798
817
  }
799
818
  }
@@ -850,9 +869,28 @@ class Program {
850
869
  }
851
870
  // Build a map of all modules within this program and the module-
852
871
  // level scope that contains the symbol table for the module.
853
- _buildModuleSymbolsMap(sourceFileToExclude, userFileOnly, includeIndexUserSymbols, token) {
872
+ _buildModuleSymbolsMap(sourceFileToExclude, libraryMap, includeSymbolsFromIndices, token) {
873
+ // If we have library map, always use the map for library symbols.
874
+ const predicate = (s) => {
875
+ if (!libraryMap) {
876
+ // We don't have any prebuilt indices, so we need to include
877
+ // all files.
878
+ return true;
879
+ }
880
+ if (!this._configOptions.indexing) {
881
+ // We have some prebuilt indices such as stdlib, but indexing is disabled.
882
+ // Include files we don't have prebuilt indices.
883
+ return libraryMap.get(s.sourceFile.getFilePath()) === undefined;
884
+ }
885
+ // We have prebuilt indices for third party libraries. Include only
886
+ // user files.
887
+ return (0, sourceFileInfoUtils_1.isUserCode)(s);
888
+ };
889
+ // Only include import alias from user files if indexing is off for now.
890
+ // Currently, when indexing is off, we don't do import alias deduplication, so
891
+ // adding import alias is cheap. But when indexing is on, we do deduplication, which
854
892
  // require resolveAliasDeclaration that can cause more files to be parsed and bound.
855
- return (0, autoImporter_1.buildModuleSymbolsMap)(this._sourceFileList.filter((s) => s !== sourceFileToExclude && (userFileOnly ? (0, sourceFileInfoUtils_1.isUserCode)(s) : true)), includeIndexUserSymbols, token);
893
+ return (0, autoImporter_1.buildModuleSymbolsMap)(this._sourceFileList.filter((s) => s !== sourceFileToExclude && predicate(s)), includeSymbolsFromIndices, token);
856
894
  }
857
895
  _shouldCheckFile(fileInfo) {
858
896
  // Always do a full checking for a file that's open in the editor.
@@ -1115,8 +1153,8 @@ class Program {
1115
1153
  return [];
1116
1154
  }
1117
1155
  const writtenWord = fileContents.substr(textRange.start, textRange.length);
1118
- const map = this._buildModuleSymbolsMap(sourceFileInfo, !!options.libraryMap,
1119
- /* includeIndexUserSymbols */ true, token);
1156
+ const map = this._buildModuleSymbolsMap(sourceFileInfo, options.libraryMap,
1157
+ /* includeSymbolsFromIndices */ true, token);
1120
1158
  options.patternMatcher =
1121
1159
  (_a = options.patternMatcher) !== null && _a !== void 0 ? _a : ((p, t) => (0, stringUtils_1.computeCompletionSimilarity)(p, t) > similarityLimit);
1122
1160
  const autoImporter = new autoImporter_1.AutoImporter(this._configOptions.findExecEnvironment(filePath), this._importResolver, parseTree, range.start, new completionProvider_1.CompletionMap(), map, options);
@@ -1335,17 +1373,6 @@ class Program {
1335
1373
  }
1336
1374
  });
1337
1375
  }
1338
- getHoverForPosition(filePath, position, format, token) {
1339
- return this._runEvaluatorWithCancellationToken(token, () => {
1340
- const sourceFileInfo = this.getSourceFileInfo(filePath);
1341
- if (!sourceFileInfo) {
1342
- return undefined;
1343
- }
1344
- this._bindFile(sourceFileInfo);
1345
- const execEnv = this._configOptions.findExecEnvironment(filePath);
1346
- return sourceFileInfo.sourceFile.getHoverForPosition(this._createSourceMapper(execEnv, token, sourceFileInfo, /* mapCompiled */ true), position, format, this._evaluator, this.functionSignatureDisplay(), token);
1347
- });
1348
- }
1349
1376
  getDocumentHighlight(filePath, position, token) {
1350
1377
  return this._runEvaluatorWithCancellationToken(token, () => {
1351
1378
  const sourceFileInfo = this.getSourceFileInfo(filePath);
@@ -1380,7 +1407,7 @@ class Program {
1380
1407
  this._bindFile(sourceFileInfo);
1381
1408
  const execEnv = this._configOptions.findExecEnvironment(filePath);
1382
1409
  sourceMapper = this._createSourceMapper(execEnv, token, sourceFileInfo, /* mapCompiled */ true);
1383
- return sourceFileInfo.sourceFile.getCompletionsForPosition(position, workspacePath, this._configOptions, this._importResolver, this._lookUpImport, this._evaluator, options, sourceMapper, nameMap, libraryMap, () => this._buildModuleSymbolsMap(sourceFileInfo, !!libraryMap, options.includeUserSymbolsInAutoImport, token), token);
1410
+ return sourceFileInfo.sourceFile.getCompletionsForPosition(position, workspacePath, this._configOptions, this._importResolver, this._lookUpImport, this._evaluator, options, sourceMapper, nameMap, libraryMap, () => this._buildModuleSymbolsMap(sourceFileInfo, libraryMap, options.includeUserSymbolsInAutoImport, token), token);
1384
1411
  });
1385
1412
  ls.add(`found ${(_a = result === null || result === void 0 ? void 0 : result.completionMap.size) !== null && _a !== void 0 ? _a : 'null'} items`);
1386
1413
  return result;
@@ -1411,7 +1438,7 @@ class Program {
1411
1438
  }
1412
1439
  this._bindFile(sourceFileInfo);
1413
1440
  const execEnv = this._configOptions.findExecEnvironment(filePath);
1414
- sourceFileInfo.sourceFile.resolveCompletionItem(this._configOptions, this._importResolver, this._lookUpImport, this._evaluator, options, this._createSourceMapper(execEnv, token, sourceFileInfo, /* mapCompiled */ true), nameMap, libraryMap, () => this._buildModuleSymbolsMap(sourceFileInfo, !!libraryMap, options.includeUserSymbolsInAutoImport, token), completionItem, token);
1441
+ sourceFileInfo.sourceFile.resolveCompletionItem(this._configOptions, this._importResolver, this._lookUpImport, this._evaluator, options, this._createSourceMapper(execEnv, token, sourceFileInfo, /* mapCompiled */ true), nameMap, libraryMap, () => this._buildModuleSymbolsMap(sourceFileInfo, libraryMap, options.includeUserSymbolsInAutoImport, token), completionItem, token);
1415
1442
  });
1416
1443
  }
1417
1444
  renameModule(path, newPath, token) {
@@ -1521,7 +1548,7 @@ class Program {
1521
1548
  const range = (0, positionUtils_1.convertTextRangeToRange)({ start: insertionPoint, length: 0 }, newFileParseResults.tokenizerOutput.lines);
1522
1549
  // If we are adding at the end of line (ex, end of a file),
1523
1550
  // add new lines.
1524
- const newLinesToAdd = _getNumberOfBlankLinesToInsert(newFileParseResults, range.end);
1551
+ const newLinesToAdd = _getNumberOfBlankLinesToInsert(newFileParseResults, sourceDecl, range.end);
1525
1552
  codeSnippetToInsert = '\n'.repeat(newLinesToAdd) + codeSnippetToInsert;
1526
1553
  renameModuleProvider.textEditTracker.addEdit(newFilePath, range, codeSnippetToInsert);
1527
1554
  }
@@ -1642,18 +1669,51 @@ class Program {
1642
1669
  realFilePath,
1643
1670
  });
1644
1671
  }
1645
- function _getNumberOfBlankLinesToInsert(parseResults, position) {
1646
- // This basically try to add 2 blanks lines before previous line with text.
1672
+ function _getNumberOfBlankLinesToInsert(parseResults, decl, position) {
1647
1673
  if (position.line === 0 && position.character === 0) {
1648
1674
  return 0;
1649
1675
  }
1650
- const linesToAdd = position.line > 0 && (0, parseTreeUtils_1.isBlankLine)(parseResults, position.line - 1)
1651
- ? position.line > 1 && (0, parseTreeUtils_1.isBlankLine)(parseResults, position.line - 2)
1652
- ? 0
1653
- : 1
1654
- : 2;
1655
- // Add one more line for the line that position is on if it is not blank.
1656
- return position.character !== 0 ? linesToAdd + 1 : linesToAdd;
1676
+ let previousStatement;
1677
+ const offset = (0, positionUtils_1.convertPositionToOffset)(position, parseResults.tokenizerOutput.lines);
1678
+ if (offset && parseResults.parseTree.statements.length > 0) {
1679
+ previousStatement = parseResults.parseTree.statements.reduce((prev, curr) => offset < curr.start ? prev : curr);
1680
+ }
1681
+ // This basically try to add some blank lines after the last line with text.
1682
+ let linesToAdd = 0;
1683
+ if (previousStatement) {
1684
+ if ((0, declaration_1.isVariableDeclaration)(decl)) {
1685
+ switch (previousStatement.nodeType) {
1686
+ case 47 /* StatementList */:
1687
+ // Small statement such as call, assignment, etc.
1688
+ linesToAdd = 0;
1689
+ break;
1690
+ case 10 /* Class */:
1691
+ case 28 /* Function */:
1692
+ linesToAdd = 2;
1693
+ break;
1694
+ default:
1695
+ // any other statement such as if, while, etc. we will add 1 blank line.
1696
+ linesToAdd = 1;
1697
+ }
1698
+ }
1699
+ else {
1700
+ linesToAdd = 2;
1701
+ }
1702
+ }
1703
+ // If the position is not at the beginning of the line, we need to add 1 more '\n'
1704
+ // to start from blank line.
1705
+ linesToAdd += position.character !== 0 ? 1 : 0;
1706
+ // If there are already blank lines, we only add the difference.
1707
+ const desiredBlankLines = linesToAdd;
1708
+ const startingLine = position.character !== 0 ? position.line : position.line - 1;
1709
+ for (let i = 0; i < desiredBlankLines; i++) {
1710
+ const currentLine = startingLine - i;
1711
+ if (currentLine < 0 || !(0, parseTreeUtils_1.isBlankLine)(parseResults, currentLine)) {
1712
+ break;
1713
+ }
1714
+ linesToAdd--;
1715
+ }
1716
+ return linesToAdd;
1657
1717
  }
1658
1718
  }
1659
1719
  clone() {
@@ -1874,7 +1934,7 @@ class Program {
1874
1934
  extensibility_1.Extensions.getProgramExtensions(this.rootPath).forEach((e) => (e.clearCache ? e.clearCache() : null));
1875
1935
  }
1876
1936
  test_createSourceMapper(execEnv, from) {
1877
- return this._createSourceMapper(execEnv, vscode_languageserver_1.CancellationToken.None, /*from*/ from, /* mapCompiled */ false);
1937
+ return this._createSourceMapper(execEnv, vscode_languageserver_1.CancellationToken.None, /* from */ from, /* mapCompiled */ false);
1878
1938
  }
1879
1939
  _getRenameSymbolMode(sourceFileInfo, referencesResult, isDefaultWorkspace) {
1880
1940
  // We have 2 different cases