Skip to content

Commit

Permalink
feat: use qt6 and qtc6
Browse files Browse the repository at this point in the history
  • Loading branch information
gikari authored and sassanh committed Apr 19, 2023
1 parent 903683d commit cd8c871
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 47 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build_cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on: [ push, pull_request ]

env:
PLUGIN_NAME: QNVim
QT_VERSION: 5.15.2
QT_VERSION: 6.2.4
CMAKE_VERSION: 3.22.4
NINJA_VERSION: 1.10.1

Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ if (MSVC)
endif()

find_package(QtCreator REQUIRED COMPONENTS Core)
find_package(Qt5 COMPONENTS Widgets Network REQUIRED)
find_package(Qt6 REQUIRED COMPONENTS Widgets Network)

add_subdirectory(src)
2 changes: 1 addition & 1 deletion external/qtcreator/version.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# SPDX-FileCopyrightText: None
# SPDX-License-Identifier: MIT

set(QT_CREATOR_VERSION "5.0.2")
set(QT_CREATOR_VERSION "6.0.2")
set(QT_CREATOR_SNAPSHOT "")
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ add_qtc_plugin(QNVim
QtCreator::TextEditor
QtCreator::ProjectExplorer
DEPENDS
Qt5::Widgets
Qt::Widgets
QtCreator::ExtensionSystem
QtCreator::Utils
neovim-qt
Expand Down
5 changes: 2 additions & 3 deletions src/QNVim.json.in
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
{
\"Name\" : \"QNVim\",
\"Version\" : \"1.2.1\",
\"CompatVersion\" : \"0.4.0\",
\"Version\" : \"6.0.2_1\",
\"Vendor\" : \"Sassan Haradji\",
\"Copyright\" : \"(C) Sassan Haradji\",
\"License\" : \"MIT\",
\"Description\" : \"Neovim Backend for Qt Creator\",
\"Url\" : \"\",
\"Url\" : \"https://github.com/sassanh/qnvim\",
$$dependencyList
}
125 changes: 85 additions & 40 deletions src/qnvimplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,21 @@
#include <coreplugin/editormanager/ieditor.h>
#include <coreplugin/icontext.h>
#include <coreplugin/statusbarmanager.h>

#include <gui/input.h>
#include <msgpackrequest.h>
#include <neovimconnector.h>

#include <projectexplorer/project.h>
#include <projectexplorer/session.h>

#include <texteditor/displaysettings.h>
#include <texteditor/fontsettings.h>
#include <texteditor/textdocument.h>
#include <texteditor/texteditor.h>
#include <texteditor/texteditorsettings.h>
#include <texteditor/tabsettings.h>

#include <utils/differ.h>
#include <utils/fancylineedit.h>
#include <utils/fileutils.h>
Expand Down Expand Up @@ -129,20 +134,28 @@ void QNVimPlugin::syncSelectionToVim(Core::IEditor *editor) {

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

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

QString visualCommand;
if (textEditor->hasBlockSelection()) {
line = QStringView(text).left(cursorPosition).count('\n') + 1;
col = text.left(cursorPosition).section('\n', -1).length() + 1;
vLine = QStringView(text).left(anchorPosition).count('\n') + 1;
vCol = text.left(anchorPosition).section('\n', -1).length() + 1;
if (mtc.hasMultipleCursors()) {
auto mainCursor = mtc.mainCursor();

// We should always use main cursor pos here,
// because it is the cursor user controls with hjkl
auto nvimPos = mainCursor.position();

// NOTE: Theoretically, it is not alwaus the case,
// that the main cursor is at the ends of mtc array,
// but for creating our own block selections it works
auto lastCursor = mainCursor == *mtc.begin() ? *(mtc.end() - 1) : *mtc.begin();
auto nvimAnchor = lastCursor.anchor();

line = QStringView(text).left(nvimPos).count('\n') + 1;
col = text.left(nvimPos).section('\n', -1).length() + 1;
vLine = QStringView(text).left(nvimAnchor).count('\n') + 1;
vCol = text.left(nvimAnchor).section('\n', -1).length() + 1;

if (vCol < col)
--col;
Expand All @@ -153,6 +166,13 @@ void QNVimPlugin::syncSelectionToVim(Core::IEditor *editor) {
} else if (mMode == "V") {
return;
} else {
auto cursor = textEditor->textCursor();
int cursorPosition = cursor.position();
int anchorPosition = cursor.anchor();

if (anchorPosition == cursorPosition)
return;

if (anchorPosition < cursorPosition)
--cursorPosition;
else
Expand Down Expand Up @@ -231,52 +251,79 @@ void QNVimPlugin::syncCursorFromVim(const QVariantList &pos, const QVariantList
mVCursor.setY(vLine);
mVCursor.setX(vCol);

int a = QString("\n" + mText).section('\n', 0, vLine - 1).length() + vCol - 1;
int p = QString("\n" + mText).section('\n', 0, line - 1).length() + col - 1;
int anchor = QString("\n" + mText).section('\n', 0, vLine - 1).length() + vCol - 1;
int position = QString("\n" + mText).section('\n', 0, line - 1).length() + col - 1;
if (mMode == "V") {
if (a < p) {
a = QString("\n" + mText).section('\n', 0, vLine - 1).length();
p = QString("\n" + mText).section('\n', 0, line).length() - 1;
if (anchor < position) {
anchor = QString("\n" + mText).section('\n', 0, vLine - 1).length();
position = QString("\n" + mText).section('\n', 0, line).length() - 1;
} else {
a = QString("\n" + mText).section('\n', 0, vLine).length() - 1;
p = QString("\n" + mText).section('\n', 0, line - 1).length();
anchor = QString("\n" + mText).section('\n', 0, vLine).length() - 1;
position = QString("\n" + mText).section('\n', 0, line - 1).length();
}

QTextCursor cursor = textEditor->textCursor();
cursor.setPosition(a);
cursor.setPosition(p, QTextCursor::KeepAnchor);
cursor.setPosition(anchor);
cursor.setPosition(position, QTextCursor::KeepAnchor);

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

} else if (mMode == "v") {
if (a > p)
++a;
if (anchor > position)
++anchor;
else
++p;
++position;

QTextCursor cursor = textEditor->textCursor();
cursor.setPosition(a);
cursor.setPosition(p, QTextCursor::KeepAnchor);
cursor.setPosition(anchor);
cursor.setPosition(position, QTextCursor::KeepAnchor);

if (textEditor->textCursor().anchor() != cursor.anchor() or
textEditor->textCursor().position() != cursor.position())
textEditor->setTextCursor(cursor);
} else if (mMode == "\x16") {
} else if (mMode == "\x16") { // VISUAL BLOCK
if (vCol > col)
++a;
++anchor;
else
++p;
++position;

QTextCursor cursor = textEditor->textCursor();
cursor.setPosition(a);
cursor.setPosition(p, QTextCursor::KeepAnchor);
textEditor->setBlockSelection(cursor);
auto document = textEditor->textCursor().document();
const auto& tabs = textEditor->textDocument()->tabSettings();

const auto firstBlock = document->findBlock(anchor);
const auto lastBlock = document->findBlock(position);
const auto localAnchor = tabs.columnAt(firstBlock.text(), anchor - firstBlock.position());
const auto localPos = tabs.columnAt(lastBlock.text(), position - lastBlock.position());

// Get next block no matter the direction of selection
auto after = [&](const auto& block) {
if (anchor < position)
return block.next();
else
return block.previous();
};

auto mtc = Utils::MultiTextCursor();
for (auto curBlock = firstBlock; curBlock != after(lastBlock); curBlock = after(curBlock)) {
auto newCursor = QTextCursor(curBlock);

auto anchorBoundOffset = tabs.positionAtColumn(curBlock.text(), localAnchor);
auto newCursorAnchor = curBlock.position() + anchorBoundOffset;
newCursor.setPosition(newCursorAnchor);

auto posBoundOffset = tabs.positionAtColumn(curBlock.text(), localPos);
auto newCursorPosition = curBlock.position() + posBoundOffset;
newCursor.setPosition(newCursorPosition, QTextCursor::KeepAnchor);

mtc.addCursor(newCursor);
}
textEditor->setMultiTextCursor(mtc);
} else {
QTextCursor cursor = textEditor->textCursor();
cursor.clearSelection();
cursor.setPosition(p);
cursor.setPosition(position);

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

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

connect(
document, &Core::IDocument::contentsChanged, this, [=]() {
connect(document, &Core::IDocument::contentsChanged, this, [=]() {
auto buffer = mBuffers[editor];
QString bufferType = mBufferType[buffer];
if (!mEditors.contains(buffer) or (bufferType != "acwrite" and !bufferType.isEmpty()))
return;
syncToVim(editor);
},
Qt::QueuedConnection);
connect(
textEditor, &TextEditor::TextEditorWidget::cursorPositionChanged, this, [=]() {
connect(textEditor, &TextEditor::TextEditorWidget::cursorPositionChanged, this, [=]() {
if (Core::EditorManager::currentEditor() != editor)
return;
QString newText = textEditor->toPlainText();
Expand All @@ -708,8 +753,7 @@ void QNVimPlugin::editorOpened(Core::IEditor *editor) {
syncCursorToVim(editor);
},
Qt::QueuedConnection);
connect(
textEditor, &TextEditor::TextEditorWidget::selectionChanged, this, [=]() {
connect(textEditor, &TextEditor::TextEditorWidget::selectionChanged, this, [=]() {
if (Core::EditorManager::currentEditor() != editor)
return;
QString newText = textEditor->toPlainText();
Expand All @@ -718,7 +762,8 @@ void QNVimPlugin::editorOpened(Core::IEditor *editor) {
syncSelectionToVim(editor);
},
Qt::QueuedConnection);
connect(textEditor->textDocument(), &TextEditor::TextDocument::fontSettingsChanged, this, &QNVimPlugin::updateCursorSize);
connect(textEditor->textDocument(), &TextEditor::TextDocument::fontSettingsChanged,
this, &QNVimPlugin::updateCursorSize);
}
mSettingBufferFromVim = 0;

Expand Down

0 comments on commit cd8c871

Please sign in to comment.