Bug 478303 - Snippet output different when using Tool View and auto-complete
Summary: Snippet output different when using Tool View and auto-complete
Status: CONFIRMED
Alias: None
Product: kate
Classification: Applications
Component: plugin-snippets (show other bugs)
Version: 23.08.3
Platform: openSUSE Linux
: NOR normal
Target Milestone: ---
Assignee: KWrite Developers
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-12-09 14:20 UTC by Lassi Väätämöinen
Modified: 2024-11-21 13:31 UTC (History)
2 users (show)

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


Attachments
C++ debug snippet showing the issue (1.05 KB, text/xml)
2023-12-09 14:20 UTC, Lassi Väätämöinen
Details
Inserted from pane with mouse: OK (52.50 KB, image/png)
2024-07-11 20:52 UTC, Lassi Väätämöinen
Details
NOK: insert with 0 spaces indent on current line (75.28 KB, image/png)
2024-07-11 20:53 UTC, Lassi Väätämöinen
Details
NOK: insert snipped at 2 spaces indent (261.27 KB, image/png)
2024-07-11 20:53 UTC, Lassi Väätämöinen
Details
OK: insert at 1 space indent, same as the above text line indent (52.39 KB, image/png)
2024-07-11 20:54 UTC, Lassi Väätämöinen
Details
Kate autocompletion settings (81.26 KB, image/png)
2024-11-06 19:02 UTC, Lassi Väätämöinen
Details
Kate: video about characers beign deleted after completion used (58.98 KB, video/webm)
2024-11-06 19:36 UTC, Lassi Väätämöinen
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Lassi Väätämöinen 2023-12-09 14:20:32 UTC
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"
Comment 1 Lassi Väätämöinen 2024-07-11 20:51:15 UTC
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
Comment 2 Lassi Väätämöinen 2024-07-11 20:52:04 UTC
Created attachment 171584 [details]
Inserted from pane with mouse: OK
Comment 3 Lassi Väätämöinen 2024-07-11 20:53:03 UTC
Created attachment 171585 [details]
NOK: insert with 0 spaces indent on current line

Also, this image shows the test snippet definition.
Comment 4 Lassi Väätämöinen 2024-07-11 20:53:48 UTC
Created attachment 171586 [details]
NOK: insert snipped at 2 spaces indent
Comment 5 Lassi Väätämöinen 2024-07-11 20:54:26 UTC
Created attachment 171587 [details]
OK: insert at 1 space indent, same as the above text line indent
Comment 6 Lassi Väätämöinen 2024-07-11 21:03:09 UTC
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?
Comment 7 Lassi Väätämöinen 2024-07-11 21:23:24 UTC
(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.
Comment 8 Christoph Cullmann 2024-11-02 20:45:15 UTC
Could you try to fix that?
Comment 9 Lassi Väätämöinen 2024-11-04 17:41:58 UTC
(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?
Comment 10 Christoph Cullmann 2024-11-04 21:50:06 UTC
(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.
Comment 11 Waqar Ahmed 2024-11-06 18:30:32 UTC
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.
Comment 12 Lassi Väätämöinen 2024-11-06 18:32:18 UTC
(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.
Comment 13 Lassi Väätämöinen 2024-11-06 19:02:40 UTC
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.
Comment 14 Waqar Ahmed 2024-11-06 19:28:46 UTC
> 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
Comment 15 Lassi Väätämöinen 2024-11-06 19:36:20 UTC
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.
Comment 16 Bug Janitor Service 2024-11-21 03:46:31 UTC
🐛🧹 ⚠️ 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!