Commit 7b80fa36 authored by Dominik Haumann's avatar Dominik Haumann

KTE::Document: chracterAt(), wordAt(), wordRangeAt()

- rename character(Cursor) to characterAt(Cursor)
- add wordRangeAt(Range)
- add wordAt(Cursor), reusing wordRangeAt()

Rename KateDocument::getWord() to wordAt().
parent bffb7e08
......@@ -191,7 +191,7 @@ KTextEditor::Range KateProjectCompletion::completionRange(KTextEditor::View* vie
KTextEditor::Document *doc = view->document();
while ( col > 0 )
{
QChar c = ( doc->character( KTextEditor::Cursor( line, col-1 ) ) );
QChar c = ( doc->characterAt( KTextEditor::Cursor( line, col-1 ) ) );
if ( c.isLetterOrNumber() || c.isMark() || c == '_' )
{
col--;
......
......@@ -444,9 +444,15 @@ The following interfaces were merged, in order:
- \p CoordinatesToCursorInterface was merged into the KTextEditor::View.
\section kte_port_rename Interface Changes
The following interfaces were renamed:
The following interfaces were changed:
- \p KTextEditor::View::setSelection(const Cursor &position, int length, bool wrap);
was removed in favour of the Range based KTextEditor::View::setSelection()
- \p KTextEditor::Document::wordRangeAt(const Cursor &position);
returns the Range of the word under \p position.
- \p KTextEditor::Document::wordAt(const Cursor &position);
returns the word under \p position.
- \p KTextEditor::Document::character(const Cursor &position); was renamed to
\p KTextEditor::Document::characterAt(const Cursor &position);
\section kte_port_new New Interfaces
The following interfaces are new:
......
......@@ -349,16 +349,13 @@ class KTEXTEDITOR_EXPORT Document : public KParts::ReadWritePart
virtual QString text ( const Range& range, bool block = false ) const = 0;
/**
* Get the character at \p cursor.
* Get the character at text position \p cursor.
* \param position the location of the character to retrieve
* \return the requested character, or QChar() for invalid cursors.
* \see setText()
* \todo KDE5: rename to characterAt() for consistency with wordAt() and wordRangeAt()
*/
virtual QChar character( const Cursor& position ) const = 0;
virtual QChar characterAt( const Cursor& position ) const = 0;
// TODO: KDE5, add wordAt(), implementation already exists in KateDocument::getWord().
// In the implementation, reuse wordRangeAt() to avoid code duplication.
/*
* Get the word at the text position \p cursor.
* The returned word is defined by the word boundaries to the left and
......@@ -374,9 +371,8 @@ class KTEXTEDITOR_EXPORT Document : public KParts::ReadWritePart
*
* \see wordRangeAt(), characterAt()
*/
//QString wordAt(const KTextEditor::Cursor& cursor) const = 0;
virtual QString wordAt(const KTextEditor::Cursor& cursor) const = 0;
// TODO: KDE5, add wordRangeAt(), implementation for the range already exists in KateDocument::getWord()
/*
* Get the text range for the word located under the text position \p cursor.
* The returned word is defined by the word boundaries to the left and
......@@ -393,7 +389,7 @@ class KTEXTEDITOR_EXPORT Document : public KParts::ReadWritePart
*
* \see wordAt(), characterAt(), KTextEditor::Range::isValid()
*/
//KTextEditor::Range wordRangeAt(const KTextEditor::Cursor& cursor) const = 0;
virtual KTextEditor::Range wordRangeAt(const KTextEditor::Cursor& cursor) const = 0;
// TODO: KDE5 add this function (Sven Brauch, Dominik Haumann, Milian Wolff)
// and use this in Kate Part's text selection to disallow seleting
......
......@@ -101,7 +101,7 @@ void DocumentCursor::makeValid()
m_cursor.setColumn(m_document->lineLength(line));
}
// TODO KDE5 if QChar::isLowSurrogate() -> move one to the left.
// } else if (m_document->character(m_cursor).isLowSurrogate()) {
// } else if (m_document->characterAt(m_cursor).isLowSurrogate()) {
// Q_ASSERT(col > 0);
// m_cursor.setColumn(col - 1);
// }
......
......@@ -292,7 +292,7 @@ KTextEditor::Range KateWordCompletionModel::completionRange(KTextEditor::View* v
KTextEditor::Document *doc = view->document();
while ( col > 0 )
{
const QChar c = ( doc->character( KTextEditor::Cursor( line, col-1 ) ) );
const QChar c = ( doc->characterAt( KTextEditor::Cursor( line, col-1 ) ) );
if ( c.isLetterOrNumber() || c.isMark() || c == '_' )
{
col--;
......
......@@ -385,7 +385,7 @@ QString KateDocument::text( const KTextEditor::Range& range, bool blockwise ) co
return s;
}
QChar KateDocument::character( const KTextEditor::Cursor & position ) const
QChar KateDocument::characterAt( const KTextEditor::Cursor & position ) const
{
Kate::TextLine textLine = m_buffer->plainLine(position.line());
......@@ -395,6 +395,33 @@ QChar KateDocument::character( const KTextEditor::Cursor & position ) const
return textLine->at(position.column());
}
QString KateDocument::wordAt( const KTextEditor::Cursor& cursor ) const
{
return text(wordRangeAt(cursor));
}
KTextEditor::Range KateDocument::wordRangeAt(const KTextEditor::Cursor& cursor) const
{
// get text line
const int line = cursor.line();
Kate::TextLine textLine = m_buffer->plainLine(line);
if ( !textLine )
return KTextEditor::Range::invalid();
// make sure the cursor is
const int lineLenth = textLine->length();
if (cursor.column() > lineLenth)
return KTextEditor::Range::invalid();
int start = cursor.column();
int end = start;
while (start > 0 && highlight()->isInWord(textLine->at(start - 1), textLine->attribute(start - 1))) start--;
while (end < lineLenth && highlight()->isInWord(textLine->at(end), textLine->attribute(end))) end++;
return KTextEditor::Range(line, start, line, end);
}
QStringList KateDocument::textLines( const KTextEditor::Range & range, bool blockwise ) const
{
QStringList ret;
......@@ -3542,22 +3569,6 @@ void KateDocument::joinLines( uint first, uint last )
editEnd();
}
QString KateDocument::getWord( const KTextEditor::Cursor& cursor )
{
int start, end, len;
Kate::TextLine textLine = m_buffer->plainLine(cursor.line());
len = textLine->length();
start = end = cursor.column();
if (start > len) // Probably because of non-wrapping cursor mode.
return QString();
while (start > 0 && highlight()->isInWord(textLine->at(start - 1), textLine->attribute(start - 1))) start--;
while (end < len && highlight()->isInWord(textLine->at(end), textLine->attribute(end))) end++;
len = end - start;
return textLine->string().mid(start, len);
}
void KateDocument::tagLines(int start, int end)
{
foreach(KateView *view,m_views)
......
......@@ -192,7 +192,9 @@ Q_SIGNALS:
virtual QStringList textLines ( const KTextEditor::Range& range, bool block = false ) const;
virtual QString text() const;
virtual QString line(int line) const;
virtual QChar character(const KTextEditor::Cursor& position) const;
virtual QChar characterAt(const KTextEditor::Cursor& position) const;
virtual QString wordAt(const KTextEditor::Cursor& cursor) const;
virtual KTextEditor::Range wordRangeAt(const KTextEditor::Cursor& cursor) const;
virtual int lines() const;
virtual KTextEditor::Cursor documentEnd() const;
virtual int totalCharacters() const;
......@@ -818,10 +820,6 @@ Q_SIGNALS:
*/
bool removeStartLineCommentFromSelection( KateView *view, int attrib=0 );
public:
// KDE5: rename to wordAt(), add wordRangeAt(), see ktexteditor/document.h
QString getWord( const KTextEditor::Cursor& cursor );
public:
void newBracketMark( const KTextEditor::Cursor& start, KTextEditor::Range& bm, int maxLines = -1 );
bool findMatchingBracket( KTextEditor::Range& range, int maxLines = -1 );
......
......@@ -249,7 +249,7 @@ KTextEditor::Cursor KateScriptDocument::anchor(int line, int column, QChar chara
// Move backwards char by char and find the opening character
while (cursor->move(-1)) {
QChar ch = document()->character(cursor->toCursor());
QChar ch = document()->characterAt(cursor->toCursor());
if (ch == lc) {
KTextEditor::Attribute::Ptr a = attributes[document()->plainKateTextLine(cursor->line())->attribute(cursor->column())];
const int ds = a->property(KateExtendedAttribute::AttributeDefaultStyleIndex).toInt();
......@@ -372,12 +372,12 @@ QString KateScriptDocument::line(int line)
QString KateScriptDocument::wordAt(int line, int column)
{
return m_document->getWord(KTextEditor::Cursor(line, column));
return m_document->wordAt(KTextEditor::Cursor(line, column));
}
QString KateScriptDocument::wordAt(const KTextEditor::Cursor& cursor)
{
return m_document->getWord(cursor);
return m_document->wordAt(cursor);
}
KTextEditor::Range KateScriptDocument::wordRangeAt(int line, int column)
......@@ -410,7 +410,7 @@ QString KateScriptDocument::charAt(int line, int column)
QString KateScriptDocument::charAt(const KTextEditor::Cursor& cursor)
{
const QChar c = m_document->character(cursor);
const QChar c = m_document->characterAt(cursor);
return c.isNull() ? "" : QString(c);
}
......@@ -439,7 +439,7 @@ bool KateScriptDocument::isSpace(int line, int column)
bool KateScriptDocument::isSpace(const KTextEditor::Cursor& cursor)
{
return m_document->character(cursor).isSpace();
return m_document->characterAt(cursor).isSpace();
}
bool KateScriptDocument::matchesAt(int line, int column, const QString &s)
......
......@@ -1449,7 +1449,7 @@ void KateSearchBar::enterIncrementalMode() {
// Still no search pattern? Use the word under the cursor
if (initialPattern.isNull()) {
const KTextEditor::Cursor cursorPosition = m_view->cursorPosition();
initialPattern = m_view->doc()->getWord( cursorPosition );
initialPattern = m_view->doc()->wordAt( cursorPosition );
}
// Create dialog
......
......@@ -262,7 +262,7 @@ void KateSpellCheckManager::trimRange(KateDocument *doc, KTextEditor::Range &r)
KTextEditor::Cursor cursor = r.start();
while(cursor < r.end()) {
if(doc->lineLength(cursor.line()) > 0
&& !doc->character(cursor).isSpace() && doc->character(cursor).category() != QChar::Other_Control) {
&& !doc->characterAt(cursor).isSpace() && doc->characterAt(cursor).category() != QChar::Other_Control) {
break;
}
cursor.setColumn(cursor.column() + 1);
......@@ -287,7 +287,7 @@ void KateSpellCheckManager::trimRange(KateDocument *doc, KTextEditor::Range &r)
cursor.setColumn(cursor.column() - 1);
}
if(cursor.column() < doc->lineLength(cursor.line())
&& !doc->character(cursor).isSpace() && doc->character(cursor).category() != QChar::Other_Control) {
&& !doc->characterAt(cursor).isSpace() && doc->characterAt(cursor).category() != QChar::Other_Control) {
break;
}
}
......
......@@ -1070,7 +1070,7 @@ bool KateViEmulatedCommandBar::handleKeyPress(const QKeyEvent* keyEvent)
QString textToInsert;
if (keyEvent->modifiers() == Qt::ControlModifier && keyEvent->key() == Qt::Key_W)
{
textToInsert = m_view->doc()->getWord(m_view->cursorPosition());
textToInsert = m_view->doc()->wordAt(m_view->cursorPosition());
}
else
{
......
......@@ -596,11 +596,11 @@ void KateViInsertMode::replayCompletion()
// Find beginning of the word.
Cursor cursorPos = m_view->cursorPosition();
Cursor wordStart = Cursor::invalid();
if (!doc()->character(cursorPos).isLetterOrNumber() && doc()->character(cursorPos) != '_')
if (!doc()->characterAt(cursorPos).isLetterOrNumber() && doc()->characterAt(cursorPos) != '_')
{
cursorPos.setColumn(cursorPos.column() - 1);
}
while (cursorPos.column() >= 0 && (doc()->character(cursorPos).isLetterOrNumber() || doc()->character(cursorPos) == '_'))
while (cursorPos.column() >= 0 && (doc()->characterAt(cursorPos).isLetterOrNumber() || doc()->characterAt(cursorPos) == '_'))
{
wordStart = cursorPos;
cursorPos.setColumn(cursorPos.column() - 1);
......@@ -608,7 +608,7 @@ void KateViInsertMode::replayCompletion()
// Find end of current word.
cursorPos = m_view->cursorPosition();
Cursor wordEnd = Cursor(cursorPos.line(), cursorPos.column() - 1);
while (cursorPos.column() < doc()->lineLength(cursorPos.line()) && (doc()->character(cursorPos).isLetterOrNumber() || doc()->character(cursorPos) == '_'))
while (cursorPos.column() < doc()->lineLength(cursorPos.line()) && (doc()->characterAt(cursorPos).isLetterOrNumber() || doc()->characterAt(cursorPos) == '_'))
{
wordEnd = cursorPos;
cursorPos.setColumn(cursorPos.column() + 1);
......
......@@ -155,7 +155,7 @@ const Range KateViModeBase::getWordRangeUnderCursor() const
Cursor c( m_view->cursorPosition() );
// find first character that is a “word letter” and start the search there
QChar ch = doc()->character( c );
QChar ch = doc()->characterAt( c );
int i = 0;
while ( !ch.isLetterOrNumber() && ! ch.isMark() && ch != '_'
&& m_extraWordCharacters.indexOf( ch) == -1 ) {
......@@ -171,7 +171,7 @@ const Range KateViModeBase::getWordRangeUnderCursor() const
}
}
ch = doc()->character( c );
ch = doc()->characterAt( c );
i++; // count characters that were advanced so we know where to start the search
}
......
......@@ -2974,7 +2974,7 @@ KateViRange KateViNormalMode::textObjectAWord()
Cursor c1 = c;
bool startedOnSpace = false;
if (doc()->character(c).isSpace())
if (doc()->characterAt(c).isSpace())
{
startedOnSpace = true;
}
......@@ -3016,7 +3016,7 @@ KateViRange KateViNormalMode::textObjectAWord()
c2 = Cursor(c2.line() + 1, 0);
swallowCarriageReturnAtEndOfLine = true;
}
const bool swallowPrecedingSpaces = (c2.column() == doc()->lineLength(c2.line()) - 1 && !doc()->character(c2).isSpace() ) || startedOnSpace || swallowCarriageReturnAtEndOfLine;
const bool swallowPrecedingSpaces = (c2.column() == doc()->lineLength(c2.line()) - 1 && !doc()->characterAt(c2).isSpace() ) || startedOnSpace || swallowCarriageReturnAtEndOfLine;
if (swallowPrecedingSpaces)
{
if (c1.column() != 0)
......@@ -3086,7 +3086,7 @@ KateViRange KateViNormalMode::textObjectAWORD()
Cursor c1 = c;
bool startedOnSpace = false;
if (doc()->character(c).isSpace())
if (doc()->characterAt(c).isSpace())
{
startedOnSpace = true;
}
......@@ -3128,7 +3128,7 @@ KateViRange KateViNormalMode::textObjectAWORD()
c2 = Cursor(c2.line() + 1, 0);
swallowCarriageReturnAtEndOfLine = true;
}
const bool swallowPrecedingSpaces = (c2.column() == doc()->lineLength(c2.line()) - 1 && !doc()->character(c2).isSpace() ) || startedOnSpace || swallowCarriageReturnAtEndOfLine;
const bool swallowPrecedingSpaces = (c2.column() == doc()->lineLength(c2.line()) - 1 && !doc()->characterAt(c2).isSpace() ) || startedOnSpace || swallowCarriageReturnAtEndOfLine;
if (swallowPrecedingSpaces)
{
if (c1.column() != 0)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment