-
-
Notifications
You must be signed in to change notification settings - Fork 511
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
Feature: end_line and end_column for api.classes.BaseName #1576
Comments
Can you give me an example? In general, how is your approach holding up? Is it good enough? It's also unclear to me where you want the end position to be. Is it at the end of the last symbol of a function? At the end of the last comment? At the start of the next symbol? |
@davidhalter I just did a proof of concept and it seems like coc is the main culprit of the weird ranges, not Jedi for the most part The only edge case / weird behavior I've found with Jedi itself is its handling of functions at the end of a file. Based on my approach, the end of a function is the 0th column on the line after the function definition. That said, if the function is the last line in a file, the last character in the function is considered the end. See the positional result of from jedi import Script
from jedi.api.classes import Name
SCRIPT = """
x = 1
def hello():
return "world"
y = 2
def goodbye():
return "world"
""".strip()
JEDI_SCRIPT = Script(code=SCRIPT)
def _document_symbol_range(name: Name):
_name = (
name._name.tree_name.get_definition() # pylint: disable=protected-access
)
(start_line, start_column) = _name.start_pos
(end_line, end_column) = _name.end_pos
print(name.full_name, "start:", _name.start_pos, "end:", _name.end_pos)
names = JEDI_SCRIPT.get_names()
for name in names:
_document_symbol_range(name) Result:
For the most part, my approach is working fine for the most part. The public API update could just be a wrapper around these private calls / maybe handling for the edge case described above. |
Why is this weird? IMO this is the only valid end position, the next line does not even exist in Jedi in the source code. |
@davidhalter It's only weird because of differs in behavior from So, either Note: I'm personally fine keeping things the way they are at this point, just wanted to point out the inconsistency in case it matters to you. |
Actually, on further consideration, for a public |
I agree. How about something like:
|
That works amazingly! Interestingly, I've found that the For reference, i've appropriated your above example into jedi-language-server and it's the first perfectly working code I have involving symbol ranges: https://github.com/pappasam/jedi-language-server/blob/v0.13.2/jedi_language_server/jedi_utils.py#L87 Many thanks for your help! |
Alright. I'm still thinking about the best function names for this:
Do you have any other ideas? |
Ugh, names are the hardest part! How about: This makes sense to me because the variables provide the start and end positions over which a Name is defined. Alternatively, both can be combined into one |
I would probably prefer Can you create a PR for that? I'm happy to merge it with a few simple tests. And if you are too lazy to write tests that's fine, too. Then I will write them.
True, haha always very annoying :) |
Great, I’ll try have something by Sunday evening, hopefully sooner |
Unfortunately, I'm having trouble running the tests locally, so I'll just push the code changes and leave the tests to you (unless you have any recommendations for me based on the debugging info below). Note: I've changed the api to
I manage Python versions with asdf, which uses pyenv under the hood. Things work fine during runtime, I just can't execute the tests. Maybe related to this, I'm not sure: #1540 I've tried this on Python 3.7 and 3.8:
|
Can you show me one stack trace? Otherwise |
@davidhalter One stack trace without
|
I'm not sure what the issue is with your environment.
Are you going to write the tests or should I? |
I’ll take a stab at it when I find a moment in the next couple days. Thanks for your help! |
Feature
When receiving a
Name
, there are use-cases where it's helpful to know theend_line
andend_column
associated with a symbol.Name already supports
line
andcolumn
, so it doesn't seem like too much of a stretch to add these two new attributes.Use case
In jedi-language-server, these values would be useful to support symbol range selection (used to create text objects based on symbols found in the document, including Functions, Classes, whatever LSP defines).
I currently support this by accessing a hidden variable, and the result isn't perfect. The
end_column
is normally0
and does not cover the full range of characters (eg, it'll omit the return statement of a function). I therefore hack it by adding a huge number in my return value. It'd be nice if Name's public API supported this so it doesn't need to be a hack.See here for this function in context: https://github.com/pappasam/jedi-language-server/blob/v0.13.1/jedi_language_server/jedi_utils.py#L87
The text was updated successfully, but these errors were encountered: