forked from qt-creator/qt-creator
Pass the correct positions to clang-format
clang-format expects the position in bytes and not in characters. This offsets the position the more non-latin1 characters the code contains. Fixes: QTCREATORBUG-21812 Fixes: QTCREATORBUG-23131 Change-Id: I499c44c5364c0623ffba7cd35e6d18659c61d742 Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
@@ -41,6 +41,7 @@
|
|||||||
#include <coreplugin/editormanager/ieditor.h>
|
#include <coreplugin/editormanager/ieditor.h>
|
||||||
#include <coreplugin/idocument.h>
|
#include <coreplugin/idocument.h>
|
||||||
#include <texteditor/formattexteditor.h>
|
#include <texteditor/formattexteditor.h>
|
||||||
|
#include <texteditor/textdocument.h>
|
||||||
#include <texteditor/texteditor.h>
|
#include <texteditor/texteditor.h>
|
||||||
#include <utils/algorithm.h>
|
#include <utils/algorithm.h>
|
||||||
#include <utils/fileutils.h>
|
#include <utils/fileutils.h>
|
||||||
@@ -48,6 +49,7 @@
|
|||||||
#include <QAction>
|
#include <QAction>
|
||||||
#include <QMenu>
|
#include <QMenu>
|
||||||
#include <QTextBlock>
|
#include <QTextBlock>
|
||||||
|
#include <QTextCodec>
|
||||||
|
|
||||||
using namespace TextEditor;
|
using namespace TextEditor;
|
||||||
|
|
||||||
@@ -106,6 +108,25 @@ void ClangFormat::formatFile()
|
|||||||
formatCurrentFile(command());
|
formatCurrentFile(command());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ClangFormat::formatAtPosition(const int pos, const int length)
|
||||||
|
{
|
||||||
|
const TextEditorWidget *widget = TextEditorWidget::currentTextEditorWidget();
|
||||||
|
if (!widget)
|
||||||
|
return;
|
||||||
|
|
||||||
|
const QTextCodec *codec = widget->textDocument()->codec();
|
||||||
|
if (!codec) {
|
||||||
|
formatCurrentFile(command(pos, length));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const QString &text = widget->textAt(0, pos + length);
|
||||||
|
const QStringView buffer(text);
|
||||||
|
const int encodedOffset = codec->fromUnicode(buffer.left(pos)).size();
|
||||||
|
const int encodedLength = codec->fromUnicode(buffer.mid(pos, length)).size();
|
||||||
|
formatCurrentFile(command(encodedOffset, encodedLength));
|
||||||
|
}
|
||||||
|
|
||||||
void ClangFormat::formatAtCursor()
|
void ClangFormat::formatAtCursor()
|
||||||
{
|
{
|
||||||
const TextEditorWidget *widget = TextEditorWidget::currentTextEditorWidget();
|
const TextEditorWidget *widget = TextEditorWidget::currentTextEditorWidget();
|
||||||
@@ -113,18 +134,16 @@ void ClangFormat::formatAtCursor()
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
const QTextCursor tc = widget->textCursor();
|
const QTextCursor tc = widget->textCursor();
|
||||||
|
|
||||||
if (tc.hasSelection()) {
|
if (tc.hasSelection()) {
|
||||||
const int offset = tc.selectionStart();
|
const int selectionStart = tc.selectionStart();
|
||||||
const int length = tc.selectionEnd() - offset;
|
formatAtPosition(selectionStart, tc.selectionEnd() - selectionStart);
|
||||||
formatCurrentFile(command(offset, length));
|
|
||||||
} else {
|
} else {
|
||||||
// Pretend that the current line was selected.
|
// Pretend that the current line was selected.
|
||||||
// Note that clang-format will extend the range to the next bigger
|
// Note that clang-format will extend the range to the next bigger
|
||||||
// syntactic construct if needed.
|
// syntactic construct if needed.
|
||||||
const QTextBlock block = tc.block();
|
const QTextBlock block = tc.block();
|
||||||
const int offset = block.position();
|
formatAtPosition(block.position(), block.length());
|
||||||
const int length = block.length();
|
|
||||||
formatCurrentFile(command(offset, length));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -185,7 +204,7 @@ void ClangFormat::disableFormattingSelectedText()
|
|||||||
// The indentation of these markers might be undesired, so reformat.
|
// The indentation of these markers might be undesired, so reformat.
|
||||||
// This is not optimal because two undo steps will be needed to remove the markers.
|
// This is not optimal because two undo steps will be needed to remove the markers.
|
||||||
const int reformatTextLength = insertCursor.position() - selectionStartBlock.position();
|
const int reformatTextLength = insertCursor.position() - selectionStartBlock.position();
|
||||||
formatCurrentFile(command(selectionStartBlock.position(), reformatTextLength));
|
formatAtPosition(selectionStartBlock.position(), reformatTextLength);
|
||||||
}
|
}
|
||||||
|
|
||||||
Command ClangFormat::command() const
|
Command ClangFormat::command() const
|
||||||
|
@@ -47,6 +47,7 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
void formatFile();
|
void formatFile();
|
||||||
|
void formatAtPosition(const int pos, const int length);
|
||||||
void formatAtCursor();
|
void formatAtCursor();
|
||||||
void formatLines();
|
void formatLines();
|
||||||
void disableFormattingSelectedText();
|
void disableFormattingSelectedText();
|
||||||
|
Reference in New Issue
Block a user