Skip to content
This repository was archived by the owner on Sep 3, 2021. It is now read-only.

Commit

Permalink
Simplify the string quotes quick fix code.
Browse files Browse the repository at this point in the history
  • Loading branch information
hugopl committed Apr 21, 2016
1 parent a4f5c1d commit 09fa3cd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
23 changes: 10 additions & 13 deletions editor/RubyQuickFixes.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "RubyQuickFixes.h"

#include <QTextBlock>
#include <QTextDocument>
#include <texteditor/codeassist/assistinterface.h>
#include <algorithm>

Expand All @@ -16,13 +16,14 @@ void SwitchStringQuotes::matchingOperations(const TextEditor::QuickFixInterface
{
QTextBlock block = interface->textDocument()->findBlock(interface->position());
QString line = block.text();
int position = interface->position() - block.position();
int userCursorPosition = interface->position();
int position = userCursorPosition - block.position();
Token token = Scanner::tokenAt(&line, position);

if (token.kind != Ruby::Token::String)
return;

SwitchStringQuotesOp* operation = new SwitchStringQuotesOp(interface->textDocument(), token.position + block.position(), token.length);
SwitchStringQuotesOp* operation = new SwitchStringQuotesOp(block, token, userCursorPosition);
QString description;
if (line[token.position] == QLatin1Char('"'))
description = QStringLiteral("Convert to single quotes");
Expand All @@ -33,17 +34,14 @@ void SwitchStringQuotes::matchingOperations(const TextEditor::QuickFixInterface
result.append(operation);
}

SwitchStringQuotesOp::SwitchStringQuotesOp(QTextDocument* document, int position, int length)
: m_document(document), m_position(position), m_length(length)
SwitchStringQuotesOp::SwitchStringQuotesOp(QTextBlock &block, const Token &token, int userCursorPosition)
: m_block(block), m_token(token), m_userCursorPosition(userCursorPosition)
{
}

void SwitchStringQuotesOp::perform()
{
QTextBlock block = m_document->findBlock(m_position);
QString line = block.text();
int linePos = m_position - block.position();
QString string = line.mid(linePos, m_length);
QString string = m_block.text().mid(m_token.position, m_token.length);

QString oldQuote = QStringLiteral("\"");
QString newQuote = QStringLiteral("'");
Expand All @@ -55,14 +53,13 @@ void SwitchStringQuotesOp::perform()
string[0] = newQuote[0];
string[string.length() - 1] = newQuote[0];

QTextCursor cursor(block);
QTextCursor cursor(m_block);
cursor.beginEditBlock();
cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::MoveAnchor, linePos);
cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, m_length);
cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::MoveAnchor, m_token.position);
cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, m_token.length);
cursor.removeSelectedText();
cursor.insertText(string);
cursor.endEditBlock();
}


}
12 changes: 7 additions & 5 deletions editor/RubyQuickFixes.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#ifndef Ruby_QuickFixes_h
#define Ruby_QuickFixes_h

#include <QTextDocument>
#include <QTextBlock>

#include <extensionsystem/iplugin.h>
#include <texteditor/quickfix.h>

#include "RubyScanner.h"

namespace Ruby {

void registerQuickFixes(ExtensionSystem::IPlugin *plugIn);
Expand All @@ -21,12 +23,12 @@ class SwitchStringQuotes : public QuickFixFactory {

class SwitchStringQuotesOp : public TextEditor::QuickFixOperation {
public:
SwitchStringQuotesOp(QTextDocument* document, int position, int length);
SwitchStringQuotesOp(QTextBlock &block, const Token &token, int userCursorPosition);
void perform() Q_DECL_OVERRIDE;
private:
QTextDocument *m_document;
int m_position;
int m_length;
QTextBlock m_block;
Token m_token;
int m_userCursorPosition;
};
}

Expand Down

0 comments on commit 09fa3cd

Please sign in to comment.