Skip to content

Commit cd8c871

Browse files
gikarisassanh
authored andcommitted
feat: use qt6 and qtc6
1 parent 903683d commit cd8c871

File tree

6 files changed

+91
-47
lines changed

6 files changed

+91
-47
lines changed

.github/workflows/build_cmake.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ on: [ push, pull_request ]
77

88
env:
99
PLUGIN_NAME: QNVim
10-
QT_VERSION: 5.15.2
10+
QT_VERSION: 6.2.4
1111
CMAKE_VERSION: 3.22.4
1212
NINJA_VERSION: 1.10.1
1313

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ if (MSVC)
2525
endif()
2626

2727
find_package(QtCreator REQUIRED COMPONENTS Core)
28-
find_package(Qt5 COMPONENTS Widgets Network REQUIRED)
28+
find_package(Qt6 REQUIRED COMPONENTS Widgets Network)
2929

3030
add_subdirectory(src)

external/qtcreator/version.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# SPDX-FileCopyrightText: None
22
# SPDX-License-Identifier: MIT
33

4-
set(QT_CREATOR_VERSION "5.0.2")
4+
set(QT_CREATOR_VERSION "6.0.2")
55
set(QT_CREATOR_SNAPSHOT "")

src/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ add_qtc_plugin(QNVim
77
QtCreator::TextEditor
88
QtCreator::ProjectExplorer
99
DEPENDS
10-
Qt5::Widgets
10+
Qt::Widgets
1111
QtCreator::ExtensionSystem
1212
QtCreator::Utils
1313
neovim-qt

src/QNVim.json.in

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
{
22
\"Name\" : \"QNVim\",
3-
\"Version\" : \"1.2.1\",
4-
\"CompatVersion\" : \"0.4.0\",
3+
\"Version\" : \"6.0.2_1\",
54
\"Vendor\" : \"Sassan Haradji\",
65
\"Copyright\" : \"(C) Sassan Haradji\",
76
\"License\" : \"MIT\",
87
\"Description\" : \"Neovim Backend for Qt Creator\",
9-
\"Url\" : \"\",
8+
\"Url\" : \"https://github.com/sassanh/qnvim\",
109
$$dependencyList
1110
}

src/qnvimplugin.cpp

Lines changed: 85 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,21 @@
1414
#include <coreplugin/editormanager/ieditor.h>
1515
#include <coreplugin/icontext.h>
1616
#include <coreplugin/statusbarmanager.h>
17+
1718
#include <gui/input.h>
1819
#include <msgpackrequest.h>
1920
#include <neovimconnector.h>
21+
2022
#include <projectexplorer/project.h>
2123
#include <projectexplorer/session.h>
24+
2225
#include <texteditor/displaysettings.h>
2326
#include <texteditor/fontsettings.h>
2427
#include <texteditor/textdocument.h>
2528
#include <texteditor/texteditor.h>
2629
#include <texteditor/texteditorsettings.h>
30+
#include <texteditor/tabsettings.h>
31+
2732
#include <utils/differ.h>
2833
#include <utils/fancylineedit.h>
2934
#include <utils/fileutils.h>
@@ -129,20 +134,28 @@ void QNVimPlugin::syncSelectionToVim(Core::IEditor *editor) {
129134

130135
auto textEditor = qobject_cast<TextEditor::TextEditorWidget *>(editor->widget());
131136
QString text = textEditor->toPlainText();
132-
auto cursor = textEditor->hasBlockSelection() ? textEditor->blockSelection() : textEditor->textCursor();
133-
int cursorPosition = cursor.position();
134-
int anchorPosition = cursor.anchor();
135-
int line, col, vLine, vCol;
136137

137-
if (anchorPosition == cursorPosition)
138-
return;
138+
auto mtc = textEditor->multiTextCursor();
139+
int line, col, vLine, vCol;
139140

140141
QString visualCommand;
141-
if (textEditor->hasBlockSelection()) {
142-
line = QStringView(text).left(cursorPosition).count('\n') + 1;
143-
col = text.left(cursorPosition).section('\n', -1).length() + 1;
144-
vLine = QStringView(text).left(anchorPosition).count('\n') + 1;
145-
vCol = text.left(anchorPosition).section('\n', -1).length() + 1;
142+
if (mtc.hasMultipleCursors()) {
143+
auto mainCursor = mtc.mainCursor();
144+
145+
// We should always use main cursor pos here,
146+
// because it is the cursor user controls with hjkl
147+
auto nvimPos = mainCursor.position();
148+
149+
// NOTE: Theoretically, it is not alwaus the case,
150+
// that the main cursor is at the ends of mtc array,
151+
// but for creating our own block selections it works
152+
auto lastCursor = mainCursor == *mtc.begin() ? *(mtc.end() - 1) : *mtc.begin();
153+
auto nvimAnchor = lastCursor.anchor();
154+
155+
line = QStringView(text).left(nvimPos).count('\n') + 1;
156+
col = text.left(nvimPos).section('\n', -1).length() + 1;
157+
vLine = QStringView(text).left(nvimAnchor).count('\n') + 1;
158+
vCol = text.left(nvimAnchor).section('\n', -1).length() + 1;
146159

147160
if (vCol < col)
148161
--col;
@@ -153,6 +166,13 @@ void QNVimPlugin::syncSelectionToVim(Core::IEditor *editor) {
153166
} else if (mMode == "V") {
154167
return;
155168
} else {
169+
auto cursor = textEditor->textCursor();
170+
int cursorPosition = cursor.position();
171+
int anchorPosition = cursor.anchor();
172+
173+
if (anchorPosition == cursorPosition)
174+
return;
175+
156176
if (anchorPosition < cursorPosition)
157177
--cursorPosition;
158178
else
@@ -231,52 +251,79 @@ void QNVimPlugin::syncCursorFromVim(const QVariantList &pos, const QVariantList
231251
mVCursor.setY(vLine);
232252
mVCursor.setX(vCol);
233253

234-
int a = QString("\n" + mText).section('\n', 0, vLine - 1).length() + vCol - 1;
235-
int p = QString("\n" + mText).section('\n', 0, line - 1).length() + col - 1;
254+
int anchor = QString("\n" + mText).section('\n', 0, vLine - 1).length() + vCol - 1;
255+
int position = QString("\n" + mText).section('\n', 0, line - 1).length() + col - 1;
236256
if (mMode == "V") {
237-
if (a < p) {
238-
a = QString("\n" + mText).section('\n', 0, vLine - 1).length();
239-
p = QString("\n" + mText).section('\n', 0, line).length() - 1;
257+
if (anchor < position) {
258+
anchor = QString("\n" + mText).section('\n', 0, vLine - 1).length();
259+
position = QString("\n" + mText).section('\n', 0, line).length() - 1;
240260
} else {
241-
a = QString("\n" + mText).section('\n', 0, vLine).length() - 1;
242-
p = QString("\n" + mText).section('\n', 0, line - 1).length();
261+
anchor = QString("\n" + mText).section('\n', 0, vLine).length() - 1;
262+
position = QString("\n" + mText).section('\n', 0, line - 1).length();
243263
}
244264

245265
QTextCursor cursor = textEditor->textCursor();
246-
cursor.setPosition(a);
247-
cursor.setPosition(p, QTextCursor::KeepAnchor);
266+
cursor.setPosition(anchor);
267+
cursor.setPosition(position, QTextCursor::KeepAnchor);
248268

249269
if (textEditor->textCursor().anchor() != cursor.anchor() or
250270
textEditor->textCursor().position() != cursor.position())
251271
textEditor->setTextCursor(cursor);
252272

253273
} else if (mMode == "v") {
254-
if (a > p)
255-
++a;
274+
if (anchor > position)
275+
++anchor;
256276
else
257-
++p;
277+
++position;
258278

259279
QTextCursor cursor = textEditor->textCursor();
260-
cursor.setPosition(a);
261-
cursor.setPosition(p, QTextCursor::KeepAnchor);
280+
cursor.setPosition(anchor);
281+
cursor.setPosition(position, QTextCursor::KeepAnchor);
262282

263283
if (textEditor->textCursor().anchor() != cursor.anchor() or
264284
textEditor->textCursor().position() != cursor.position())
265285
textEditor->setTextCursor(cursor);
266-
} else if (mMode == "\x16") {
286+
} else if (mMode == "\x16") { // VISUAL BLOCK
267287
if (vCol > col)
268-
++a;
288+
++anchor;
269289
else
270-
++p;
290+
++position;
271291

272-
QTextCursor cursor = textEditor->textCursor();
273-
cursor.setPosition(a);
274-
cursor.setPosition(p, QTextCursor::KeepAnchor);
275-
textEditor->setBlockSelection(cursor);
292+
auto document = textEditor->textCursor().document();
293+
const auto& tabs = textEditor->textDocument()->tabSettings();
294+
295+
const auto firstBlock = document->findBlock(anchor);
296+
const auto lastBlock = document->findBlock(position);
297+
const auto localAnchor = tabs.columnAt(firstBlock.text(), anchor - firstBlock.position());
298+
const auto localPos = tabs.columnAt(lastBlock.text(), position - lastBlock.position());
299+
300+
// Get next block no matter the direction of selection
301+
auto after = [&](const auto& block) {
302+
if (anchor < position)
303+
return block.next();
304+
else
305+
return block.previous();
306+
};
307+
308+
auto mtc = Utils::MultiTextCursor();
309+
for (auto curBlock = firstBlock; curBlock != after(lastBlock); curBlock = after(curBlock)) {
310+
auto newCursor = QTextCursor(curBlock);
311+
312+
auto anchorBoundOffset = tabs.positionAtColumn(curBlock.text(), localAnchor);
313+
auto newCursorAnchor = curBlock.position() + anchorBoundOffset;
314+
newCursor.setPosition(newCursorAnchor);
315+
316+
auto posBoundOffset = tabs.positionAtColumn(curBlock.text(), localPos);
317+
auto newCursorPosition = curBlock.position() + posBoundOffset;
318+
newCursor.setPosition(newCursorPosition, QTextCursor::KeepAnchor);
319+
320+
mtc.addCursor(newCursor);
321+
}
322+
textEditor->setMultiTextCursor(mtc);
276323
} else {
277324
QTextCursor cursor = textEditor->textCursor();
278325
cursor.clearSelection();
279-
cursor.setPosition(p);
326+
cursor.setPosition(position);
280327

281328
if (textEditor->textCursor().position() != cursor.position() or
282329
textEditor->textCursor().hasSelection())
@@ -689,17 +736,15 @@ void QNVimPlugin::editorOpened(Core::IEditor *editor) {
689736

690737
Core::IDocument *document = editor->document();
691738

692-
connect(
693-
document, &Core::IDocument::contentsChanged, this, [=]() {
739+
connect(document, &Core::IDocument::contentsChanged, this, [=]() {
694740
auto buffer = mBuffers[editor];
695741
QString bufferType = mBufferType[buffer];
696742
if (!mEditors.contains(buffer) or (bufferType != "acwrite" and !bufferType.isEmpty()))
697743
return;
698744
syncToVim(editor);
699745
},
700746
Qt::QueuedConnection);
701-
connect(
702-
textEditor, &TextEditor::TextEditorWidget::cursorPositionChanged, this, [=]() {
747+
connect(textEditor, &TextEditor::TextEditorWidget::cursorPositionChanged, this, [=]() {
703748
if (Core::EditorManager::currentEditor() != editor)
704749
return;
705750
QString newText = textEditor->toPlainText();
@@ -708,8 +753,7 @@ void QNVimPlugin::editorOpened(Core::IEditor *editor) {
708753
syncCursorToVim(editor);
709754
},
710755
Qt::QueuedConnection);
711-
connect(
712-
textEditor, &TextEditor::TextEditorWidget::selectionChanged, this, [=]() {
756+
connect(textEditor, &TextEditor::TextEditorWidget::selectionChanged, this, [=]() {
713757
if (Core::EditorManager::currentEditor() != editor)
714758
return;
715759
QString newText = textEditor->toPlainText();
@@ -718,7 +762,8 @@ void QNVimPlugin::editorOpened(Core::IEditor *editor) {
718762
syncSelectionToVim(editor);
719763
},
720764
Qt::QueuedConnection);
721-
connect(textEditor->textDocument(), &TextEditor::TextDocument::fontSettingsChanged, this, &QNVimPlugin::updateCursorSize);
765+
connect(textEditor->textDocument(), &TextEditor::TextDocument::fontSettingsChanged,
766+
this, &QNVimPlugin::updateCursorSize);
722767
}
723768
mSettingBufferFromVim = 0;
724769

0 commit comments

Comments
 (0)