Created attachment 164039 [details] C++ debug snippet showing the issue SUMMARY When executing snippet plugin using auto-completion, the start of the output is left out. Using the Tool View with mouse gives full output. STEPS TO REPRODUCE 1. Add "C++ Debug.xml" snippets to Kate (into ~/.local/share/ktexteditor_snippets/data/ ) 2. Restart kate 3. Enable snippets plugin and tool view. 4. Create the following line in a C++ file 5. Use snippet Snippet outputs 1. With clicking on the Snippets Tool View on the right std::cout << "foo: " << foo << "bar: " << bar << "baz: " << baz << std::endl; 2. Using the auto-completion popup ::cout << "foo: " << foo << "bar: " << bar << "baz: " << baz << std::endl; OBSERVED RESULT "std::" missing, when triggering the snippet from auto-complete popup. EXPECTED RESULT Outputs are the same SOFTWARE/OS VERSIONS Operating System: openSUSE Tumbleweed 20231207 KDE Plasma Version: 5.27.10 KDE Frameworks Version: 5.112.0 Qt Version: 5.15.11 Kernel Version: 6.6.3-1-default (64-bit) Graphics Platform: X11 Processors: 12 × AMD Ryzen 5 3600 6-Core Processor Memory: 15,5 GiB of RAM Graphics Processor: AMD Radeon RX 580 Series Manufacturer: Micro-Star International Co., Ltd. Product Name: MS-7B79 System Version: 4.0 ADDITIONAL INFORMATION It seems the auto-complete cuts *three* first characters at the beginning: Editing the snippet script function cout_selected_vars() by adding characters at the beginning of "std::cout" -> "foobarstd::cout" you get as start of the output "barstd::cout"
OK, so the use case that causes the issue is identified. There's a bit of difference how stuff works: 1. When snippet selected from the pane: regardless of how much the cursor has been indented on an empty line (in a C++ code file), selecting a snippet from the pane causes the line to be aligned to same indentation as the above line. 2. When snippet is inserted by using the completion pop-up, the error occurs if the cursor is indented to any other position than the line above: E.g. the above line is indented 1 space. Following scenarios: - Insert snippet using completion, 0 space indent on the current line - NOK - Insert snippet using completion, 1 space indent on the current line - OK - Insert snippet using completion, 2 spaces indent on the current line - NOK NOTE: In the "NOK" cases, the inserted snippet is anyhow adjusted to the previous line indent depth: 1 space. But in these cases the inserting causes wrong character sequence at the start of the text. This is NOT the case when selecting from the side pane with mouse
Created attachment 171584 [details] Inserted from pane with mouse: OK
Created attachment 171585 [details] NOK: insert with 0 spaces indent on current line Also, this image shows the test snippet definition.
Created attachment 171586 [details] NOK: insert snipped at 2 spaces indent
Created attachment 171587 [details] OK: insert at 1 space indent, same as the above text line indent
Snippet insertion in case of mouse click happens in snippetview.cpp: void SnippetView::slotSnippetClicked(const QModelIndex &index) { QStandardItem *item = SnippetStore::self()->itemFromIndex(m_proxy->mapToSource(index)); if (!item) { return; } Snippet *snippet = Snippet::fromItem(item); if (!snippet) { return; } m_plugin->insertSnippet(snippet); } // And then in katesnippetglobal.cpp: void KateSnippetGlobal::insertSnippet(Snippet *snippet) { // query active view, always prefer that! KTextEditor::View *view = KTextEditor::Editor::instance()->application()->activeMainWindow()->activeView(); // fallback to stuff set for dialog if (!view) { view = m_activeViewForDialog; } // no view => nothing to do if (!view) { return; } // try to insert snippet SnippetCompletionItem item(snippet, static_cast<SnippetRepository *>(snippet->parent())); item.execute(view, KTextEditor::Range(view->cursorPosition(), view->cursorPosition())); // set focus to view view->setFocus(); } In case of using the text completion for inserting the snippet, it looks somewhat different in snippetcompletionitem.cpp: void SnippetCompletionItem::execute(KTextEditor::View *view, const KTextEditor::Range &word) { // insert snippet content view->insertTemplate(view->cursorPosition(), m_snippet, m_repo->script()); view->document()->removeText(word); } Maybe these both should do it in more similar way?
(In reply to Lassi Väätämöinen from comment #6) > Maybe these both should do it in more similar way? Ah, missed this earlier; so for the keyboard completion popup is invoked from KTextEditor side: bool KateCompletionWidget::execute() { ... model->executeCompletionItem(view(), *m_completionRanges[model].range, toExecute); ... } And then in snippetcompletionmodel.cpp: void SnippetCompletionModel::executeCompletionItem(KTextEditor::View *view, const KTextEditor::Range &word, const QModelIndex &index) const { if (index.parent().isValid()) { m_snippets[index.row()]->execute(view, word); } } Eventually running the same SnippetCompletionItem::execute(...) as the mouse-selection path. But still, a bit differently.
Could you try to fix that?
(In reply to Christoph Cullmann from comment #8) > Could you try to fix that? I guess it might be just the matter of duplicating the auto-completion snippet functionality with the functionality that the tool view has. Or better yet, the functionality code would be in one place, and both would execute the same code. I think that was the problem I left this at; where should the common code go?
(In reply to Lassi Väätämöinen from comment #9) > (In reply to Christoph Cullmann from comment #8) > > Could you try to fix that? > > I guess it might be just the matter of duplicating the auto-completion > snippet functionality with the functionality that the tool view has. > Or better yet, the functionality code would be in one place, and both would > execute the same code. > > I think that was the problem I left this at; where should the common code go? Good question, I have no good answer for that.
can you share a screenshot of your completion settings? (Editing -> Autocompletion) The code execute for when you manually click vs auto-completion is same. The only thing that is different is the "word" being removed. In the case of manual, nothing is removed, in auto complete case it _has_ to remove _something_ as there might be partially typed data there. Also, have you tried the bug with latest Kate? I can't reproduce the problems you are seeing. Our indenters got a lot better and a lot of other things changed in ktexteditor so this might have fixed this.
(In reply to Waqar Ahmed from comment #11) > Also, have you tried the bug with latest Kate? I can't reproduce the > problems you are seeing. Our indenters got a lot better and a lot of other > things changed in ktexteditor so this might have fixed this. I'll check when I have time. Just got my build working again yesterday, after a while.
Created attachment 175595 [details] Kate autocompletion settings (In reply to Waqar Ahmed from comment #11) > can you share a screenshot of your completion settings? (Editing -> > Autocompletion) Attached. > Also, have you tried the bug with latest Kate? I can't reproduce the > problems you are seeing. Our indenters got a lot better and a lot of other > things changed in ktexteditor so this might have fixed this. Seems to still occur. If you type one charcter, the typed character will erase the pre-selected text (as is normal with editing any field or text). BUT: If you have pre-selected text, and just directly select from the auto-complete box, the inserted result completion will have characters removed at the beginning of the line.
> BUT: If you have pre-selected text, and just directly select from the auto-complete box, the inserted result completion will have characters removed at the beginning of the line. What do you mean by this, can you explain / show? Also, would be great if you can try https://invent.kde.org/utilities/kate/-/merge_requests/1644
Created attachment 175596 [details] Kate: video about characers beign deleted after completion used (In reply to Waqar Ahmed from comment #14) > What do you mean by this, can you explain / show? See attached video for demo. The first completion is done after selecting the text 'lassi'. Second completion is done on the empty following line, which shows what the actual completion should be.
🐛🧹 ⚠️ This bug has been in NEEDSINFO status with no change for at least 15 days. Please provide the requested information, then set the bug status to REPORTED. If there is no change for at least 30 days, it will be automatically closed as RESOLVED WORKSFORME. For more information about our bug triaging procedures, please read https://community.kde.org/Guidelines_and_HOWTOs/Bug_triaging. Thank you for helping us make KDE software even better for everyone!