Bug 477759 - After formatting, the code position is reset to top of file
Summary: After formatting, the code position is reset to top of file
Status: CONFIRMED
Alias: None
Product: kdevelop
Classification: Applications
Component: All editors (other bugs)
Version First Reported In: 5.13.240180
Platform: Debian testing Linux
: NOR minor
Target Milestone: ---
Assignee: kdevelop-bugs-null
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-11-30 10:12 UTC by rusconi
Modified: 2024-01-29 12:55 UTC (History)
1 user (show)

See Also:
Latest Commit:
Version Fixed/Implemented In:
Sentry Crash Report:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description rusconi 2023-11-30 10:12:51 UTC
Greetings, 

I use the clang formatter in KDevelop. I also open many editing views with the split feature. Oftentimes I view multiple times the same file at different locations in these views. When I run the code reformatting feature, the view that is active stays at the right location while the other views change the position of the text to the top of that file's text. This is problematic because it obliges me to look back for the right position each time (or setting / unsetting continuously bookmarks). I run the code formatter very very often because the view of properly formatted code helps me understanding the code I am working on. But if that formatting comes at the price of having to always search back the original position in the file, that's a huge toll.

Would it be possible to fix this issue ?

Thank you for listening and congrats on this wonderful development enviroment!
Filippo
Comment 1 Igor Kushnir 2023-11-30 13:20:41 UTC
The relevant code appears to be in SourceFormatterController::FileFormatter::formatDocument() at https://invent.kde.org/kdevelop/kdevelop/-/blob/8f23c66e87587db3116d81f14e7d7ea3caf03150/kdevplatform/shell/sourceformattercontroller.cpp#L729-735

I think the bug can be fixed by introducing new API QVector<KTextEditor::Cursor> IDocument::allCursorPositions() and void IDocument::setAllCursorPositions(const QVector<KTextEditor::Cursor> &); and using the new API in formatDocument() to save and restore the cursor positions of all views, not just the active one. Since this API does not exist yet, similar bugs are possible elsewhere.

A merge request is welcome.
Comment 2 rusconi 2023-11-30 17:57:57 UTC
(In reply to Igor Kushnir from comment #1)
> The relevant code appears to be in
> SourceFormatterController::FileFormatter::formatDocument() at
> https://invent.kde.org/kdevelop/kdevelop/-/blob/
> 8f23c66e87587db3116d81f14e7d7ea3caf03150/kdevplatform/shell/
> sourceformattercontroller.cpp#L729-735
> 
> I think the bug can be fixed by introducing new API
> QVector<KTextEditor::Cursor> IDocument::allCursorPositions() and void
> IDocument::setAllCursorPositions(const QVector<KTextEditor::Cursor> &); and
> using the new API in formatDocument() to save and restore the cursor
> positions of all views, not just the active one. Since this API does not
> exist yet, similar bugs are possible elsewhere.
> 
> A merge request is welcome.

I would be eager to help. Please, give me some directions as to where I find the different views of a given opened file, to start with. Then, I'll try to find my way. 
Thank you,
Filippo
Comment 3 Igor Kushnir 2023-11-30 18:21:13 UTC
(In reply to rusconi from comment #2)
> Please, give me some directions as to where I find
> the different views of a given opened file, to start with. Then, I'll try to
> find my way. 
TextDocument::cursorPosition() and TextDocument::setCursorPosition() (https://invent.kde.org/kdevelop/kdevelop/-/blob/5f6282cbb7e4ded41aca76596095a7fb28e4893e/kdevplatform/shell/textdocument.cpp#L449-L479) obtain an active view via TextDocument::activeTextView(), which looks for the active view in QList<View*> Sublime::Document::views(). If I understand correctly, you just need to obtain/set cursors for all elements of this list of views.
Comment 4 rusconi 2024-01-29 12:55:28 UTC
Greetings,

I have made a pull request at https://invent.kde.org/kdevelop/kdevelop/-/merge_requests/530 that apparently sets the formatted file's views' cursor positions correctly right after the formatting of the file. I have left comments using cout(), of course remove them freely.

In the hope this very first contribution is a good starting point to have code in the upstream source tree.

Sincerely,
Filippo