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

#6370 Add line numbers to sections and text-blocks #6371

Merged
merged 30 commits into from
May 7, 2024

Conversation

ahmedyarub
Copy link
Contributor

Checklist

  • I have filed an issue about this change and discussed potential changes with the maintainers.
  • I have received the approval from the maintainers to make this change.
  • This is not a stylistic, refactoring, or cleanup change.

Please note that the maintainers will not be reviewing this change until all checkboxes are ticked. See
the Contributions section in the README for more
details.

Discussion thread for this change

Issue number: 6370

Description of this change

Add line numbers to the text blocks and sections so that we can identify them from a PsiElement. It is obvious that not in all cases we know the line number: when we are generating a new file, the line numbers are only known when the file is actually generated. In these cases I set the line number to -1 knowing that before the object is used it will go through the print() command. There, I would update the line number of the element.
If at the moment of the change (ex in my target management feature I already know the line number of the directory entry to change), then there is no need to assign a dummy value first.

Add line numbers to text blocks and sections.
Create removeAll() method to remove all the instances of an item regardless of the line number
@github-actions github-actions bot added product: CLion CLion plugin product: IntelliJ IntelliJ plugin product: GoLand GoLand plugin awaiting-review Awaiting review from Bazel team on PRs labels Apr 7, 2024
@ahmedyarub ahmedyarub changed the title Ay/section line number 6370 Add line numbers to sections and text-blocks Apr 7, 2024
@ahmedyarub ahmedyarub changed the title 6370 Add line numbers to sections and text-blocks #6370 Add line numbers to sections and text-blocks Apr 7, 2024
@ahmedyarub ahmedyarub mentioned this pull request Apr 7, 2024
3 tasks
Copy link
Collaborator

@blorente blorente left a comment

Choose a reason for hiding this comment

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

If we're using -1 to mark "uninitialized", could we make it a constant somewhere? I have a short memory, I'm sure I'll forget what -1 means 6months from now when I come back to this code.

@blorente
Copy link
Collaborator

blorente commented Apr 8, 2024

Additionally, I'm a bit scared of printing having side effects on the data structure.
Could we instead traverse the data structure in ProjectView.Builder.build() and assign line numbers then? That way we make sure that:

  • Printing doesn't modify the structure.
  • We always have line numbers when we build a project view, whether we print it or not.

@blorente
Copy link
Collaborator

blorente commented Apr 8, 2024

And, because I forgot to start with this: Thank you for your contributions! Most maintainers here have limited time to implement cool features in the plugin, and it's delightful to see the plugin improving.

Remove redundant method
Add constant for temporary line numbers
@ahmedyarub
Copy link
Contributor Author

If we're using -1 to mark "uninitialized", could we make it a constant somewhere? I have a short memory, I'm sure I'll forget what -1 means 6months from now when I come back to this code.

I was about to do that and somehow forgot about it! Done.

@ahmedyarub
Copy link
Contributor Author

Additionally, I'm a bit scared of printing having side effects on the data structure. Could we instead traverse the data structure in ProjectView.Builder.build() and assign line numbers then? That way we make sure that:

  • Printing doesn't modify the structure.
  • We always have line numbers when we build a project view, whether we print it or not.

What you are mentioning is the ideal solution beyond any doubt. The problem is that each print statement decides on its own how many new lines to add. Moving the logic to the builder means consolidating all that "new-line logic" into the ProjectView class. Can we guarantee that future printers would be compatible with ProjectView? I mean we can at least add a comment to the print() method in the interface saying that line numbers should be changed when printing which would be easily visible for devs.

@ahmedyarub
Copy link
Contributor Author

And, because I forgot to start with this: Thank you for your contributions! Most maintainers here have limited time to implement cool features in the plugin, and it's delightful to see the plugin improving.

I really appreciate your support and patience you and especially @tpasternak . I assure you that there will be much bigger changes to come.

@ahmedyarub
Copy link
Contributor Author

OK so I removed line number calculation from all the print() methods and then tested the plugin behavior thoroughly. I found that all the code-paths call the parseProjectView() after writing to the file multiple times so there is no need to calculate the line numbers when the file is being created or edited.

Copy link
Collaborator

@blorente blorente left a comment

Choose a reason for hiding this comment

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

Thanks for making the changes!

Given the flow you've proposed, I'm not sure we need TEMPORARY_LINE_NUMBER anymore. In fact, I'm not sure we need to pass line numbers to the individual methods of the Builders of different sections.

If I understand correctly, every creation and update to a ProjectView goes through a Builder. If we pass the initial line index to a Builder, it should have enough information to assign line numbers based on the operation and the sections it contains.

So, instead of having Builder.add(T element, int lineIdx), we'd have Builder.add(T element) and the Builder would take care of calculating which line numbers T should occupy.

I think this encapsulates the behaviour of adding line numbers neatly into the Builders, so that the calling code doesn't have to keep that state.

What do you think?

ahmedyarub and others added 11 commits April 23, 2024 07:55
…ns/DirectorySection.java

Co-authored-by: Borja Lorente <[email protected]>
…ns/AutomaticallyDeriveTargetsSection.java

Co-authored-by: Borja Lorente <[email protected]>
Remove line calculation from print() methods
Remove redundant method
Update comment
@ahmedyarub
Copy link
Contributor Author

Thanks for making the changes!

Given the flow you've proposed, I'm not sure we need TEMPORARY_LINE_NUMBER anymore. In fact, I'm not sure we need to pass line numbers to the individual methods of the Builders of different sections.

If I understand correctly, every creation and update to a ProjectView goes through a Builder. If we pass the initial line index to a Builder, it should have enough information to assign line numbers based on the operation and the sections it contains.

So, instead of having Builder.add(T element, int lineIdx), we'd have Builder.add(T element) and the Builder would take care of calculating which line numbers T should occupy.

I think this encapsulates the behaviour of adding line numbers neatly into the Builders, so that the calling code doesn't have to keep that state.

What do you think?

I tried doing that but found multiple instances where the builder is instantiated and used without having known line numbers. The line numbers are mostly defined when the print() method is called, and that's why we are depending on reparsing to replace the temporary line numbers with real one. I can't think of a way for example to make that change to AddDirectoryToProjectAction.doOKAction() and GenerateFromBuildFileSelectProjectViewOption.guessProjectViewFromLocation(). How do you suggest doing these changes in these methods?

@ahmedyarub
Copy link
Contributor Author

@tpasternak @blorente is there anything else that I need to do here?

Copy link
Collaborator

@blorente blorente left a comment

Choose a reason for hiding this comment

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

I finally got a little bit of time to re-review this.
It seems we can't get more invariants from the codebase as it is, so I think it's fine to merge this and iterate later on how we can remove TEMPORARY_LINE_NUMBER.

Approving with the caveat that I'd love to see more tests, I'll be happy to merge once the tests are there.

@ahmedyarub
Copy link
Contributor Author

@blorente is there any other requirement for merging?

@blorente
Copy link
Collaborator

blorente commented May 7, 2024

Not as far as I can tell, thanks for the contribution, and apologies for the slow review turnarounds!

@blorente blorente merged commit 560f899 into bazelbuild:master May 7, 2024
6 checks passed
@github-actions github-actions bot removed the awaiting-review Awaiting review from Bazel team on PRs label May 7, 2024
@ahmedyarub ahmedyarub deleted the ay/section_line_number branch May 7, 2024 16:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
product: CLion CLion plugin product: GoLand GoLand plugin product: IntelliJ IntelliJ plugin
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants