Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing slow paste with spinner #61032

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/services/preparePasteEdits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ import {
forEachChild,
getTokenAtPosition,
isIdentifier,
isImportClause,
isImportDeclaration,
isImportEqualsDeclaration,
isImportSpecifier,
isNamedImports,
isNamespaceImport,
rangeContainsPosition,
rangeContainsRange,
SourceFile,
Expand All @@ -25,6 +31,12 @@ export function preparePasteEdits(
ancestorNode => rangeContainsRange(ancestorNode, range),
);
if (!enclosingNode) return;
if (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do all these conditions work if only a an identifier is selected? What about part of the module specifier? The test only shows the case where an entire import declaration is selected.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, if only an identifier is selected, it works too because it doesn't enter this if block, instead tries to enter the forEachChild but since it doesn't have any children it just returns and moves to the next iteration. Same for moduleSpecifier. If this is selected { foo }, then it will enter the if block.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add tests to show that it works?

isImportSpecifier(enclosingNode) || isImportClause(enclosingNode) || isNamespaceImport(enclosingNode)
|| isImportEqualsDeclaration(enclosingNode) || isImportDeclaration(enclosingNode) || isNamedImports(enclosingNode)
) {
return;
}
forEachChild(enclosingNode, function checkNameResolution(node) {
if (shouldProvidePasteEdits) return;
if (isIdentifier(node) && rangeContainsPosition(range, node.getStart(sourceFile))) {
Expand Down
3 changes: 3 additions & 0 deletions tests/cases/fourslash/preparePasteEdits_returnFalse.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
/// <reference path='./fourslash.ts' />

// @Filename: /file1.ts
//// [|import {abc} from "./file2";|]
//// [|const a = 1;|]
//// [|function foo() {
//// console.log("testing");}|]
//// [|//This is a comment|]

// @Filename: /file2.ts
//// export const abc = 1;
verify.preparePasteEdits({
copiedFromFile: "/file1.ts",
copiedTextRange: test.ranges(),
Expand Down
Loading