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

Improve handling of public symbols for imports #1178

Merged
merged 3 commits into from
Nov 16, 2020

Conversation

cdce8p
Copy link
Contributor

@cdce8p cdce8p commented Nov 16, 2020

  • Don't exclude CamelCase variables / type aliases
  • Exclude protected and private constants

See: microsoft/pylance-release#606

@erictraut
Copy link
Collaborator

Nice! And thanks for providing the unit tests too.

@erictraut
Copy link
Collaborator

@heejaechang, could you please review this change?

@@ -279,11 +279,11 @@ export class AutoImporter {

if (
!isStubOrHasInit.isStub &&
autoImportSymbol.kind === CompletionItemKind.Variable &&
/[a-z]/.test(name)
(autoImportSymbol.kind === undefined || autoImportSymbol.kind === CompletionItemKind.Variable) &&
Copy link
Collaborator

Choose a reason for hiding this comment

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

which Symbol Kind is "autoImportSymbol.kind === undefined" for?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I am asking since now it will check every symbol rather than only variable symbol kind.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think what you want is tweaking this (

), not kind === undefined.

Copy link
Collaborator

@heejaechang heejaechang Nov 16, 2020

Choose a reason for hiding this comment

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

existing code basically says, if pyright already figured out that variable is "Final" or "Const", then we will always show it in auto-import regardless of how the name is written (there is some corner cases if it is from indexer, but we can ignore that one for now)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see. Originally I noticed that protected constants where also showing up, which is why I added that check. However after you mentioning it, I now believe that it might be for the better the way constants are currently handled. Unlike with variables, there are at least some scenarios in which you might what to import them.

I'll change it.

autoImportSymbol.kind === CompletionItemKind.Variable &&
/[a-z]/.test(name)
(autoImportSymbol.kind === undefined || autoImportSymbol.kind === CompletionItemKind.Variable) &&
!SymbolNameUtils.isPublicConstantOrTypeAlias(name)
Copy link
Collaborator

@heejaechang heejaechang Nov 16, 2020

Choose a reason for hiding this comment

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

previously, name check was negative check based on the fact that the lexer has already used proper identifier check (https://github.com/microsoft/pyright/blob/master/packages/pyright-internal/src/parser/characters.ts#L52) which allows non enlgish unicode as variable name.

now, only English name can be used in auto import variable. probably okay? but want to make sure that everyone is okay with that.

ex) before one could have 검은색상값 = "BLACK" and it will show up in auto import, now it won't.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@heejaechang, good question. Are you familiar with the naming conventions used in locales that don't use Roman (ASCII) characters? Would it be common to see someone use "검은색상값" as the name of a type alias, or would that be unexpected and atypical? If it's expected, then is there a common heuristic we could use here? How about "starts with a capital letter and has no underscores?"

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think using non-roman character as a variable name is not common (in Korea at least) since usually, tool support is so bad even if language spec allows it :) but that being said, I believe in some countries (one of them is Japan I believe), it is used more often.

but, this is only for auto-import, but not actual variable name, so might be okay?

Copy link
Collaborator

Choose a reason for hiding this comment

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

OK, sounds good to me. Let's stick with this for now. If it's too restrictive, I'm sure we'll get feedback.

@heejaechang heejaechang merged commit 3f982c2 into microsoft:master Nov 16, 2020
@cdce8p cdce8p deleted the import-symbols branch November 16, 2020 23:56
heejaechang added a commit to heejaechang/pyright that referenced this pull request Nov 3, 2021
…added a fourslash test for it. (microsoft#1178)

* added partial stub package fourslash test and initial work to map back mapped file path to original file path. currenly, only go to definition and find all references (rename) is handled.

* added reverse map and rename

* revert back converting code

* made test pass

* make sure all incoming and outgoing LSP requests are converted to mapped and original file paths. this is done through convertPathToUri and convertUriToPath since all LSP request is based on uril but internally we deal with everything as local filepath.

* fix prettier issue

* handle partial stub file opened out of band

* make sure getOriginalFilePath/MapFilePath to follow the chain

and PR feedback

* clean ups

* call convertUriToPath rather than GetMppaedFile

* fixed merge conflicts

* move workspaceEdits its own file

* remove mapped file concept from file system and get it directly from pyrightFS

* fixed build error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants