Git commit b8a59d8eb6f311f5bcd4d39ac1951801900532e4 by Michel Ludwig.
Committed on 23/03/2013 at 18:17.
Pushed by mludwig into branch 'master'.

Add an optional argument to 'EditorExtension::selectParagraph()' to return the 
precise selection of the paragraph (without superfluous whitespace)

Patch by Eugene Shalygin.

M  +3    -3    doc/scripting.docbook
M  +40   -8    src/editorextension.cpp
M  +3    -2    src/editorextension.h
M  +2    -3    src/scripting/kilescriptview.cpp
M  +1    -1    src/scripting/kilescriptview.h

http://commits.kde.org/kile/b8a59d8eb6f311f5bcd4d39ac1951801900532e4

diff --git a/doc/scripting.docbook b/doc/scripting.docbook
index 9e87bee..b2314fd 100644
--- a/doc/scripting.docbook
+++ b/doc/scripting.docbook
@@ -664,10 +664,10 @@ Selects the text of the current math group.
 
 <variablelist><varlistentry>
 <term><synopsis>
-void view.selectParagraph();
+void view.selectParagraph(bool wholeLines = true);
 </synopsis></term>
 <listitem><para>
-Selects the entire text of the current &latex; paragraph.
+Selects the entire text of the current &latex; paragraph. If 
<parameter>wholeLines</parameter> is <literal>true</literal>, the first and the 
last lines of the paragraph will be included in the selection entirely 
(including the end-of-line character); otherwise, the selection will only 
contain non-whitespace characters.
 </para></listitem>
 </varlistentry></variablelist>
 
@@ -2028,4 +2028,4 @@ function surroundTexCommand(cmd,r)
 
 </sect1>
 
-</chapter>
\ No newline at end of file
+</chapter>
diff --git a/src/editorextension.cpp b/src/editorextension.cpp
index fc3906b..33b95ca 100644
--- a/src/editorextension.cpp
+++ b/src/editorextension.cpp
@@ -2262,14 +2262,14 @@ QString EditorExtension::word(const KTextEditor::Cursor 
&cursor, bool latexComma
 
 //////////////////// paragraph ////////////////////
 
-void EditorExtension::selectParagraph(KTextEditor::View *view)
+void EditorExtension::selectParagraph(KTextEditor::View* view, bool wholeLines)
 {
        view = determineView(view);
        if(!view) {
                return;
        }
 
-       KTextEditor::Range range = findCurrentParagraphRange(view);
+       KTextEditor::Range range = findCurrentParagraphRange(view, wholeLines);
        if ( range.isValid() ) {
                view->setSelection(range);
        }
@@ -2298,17 +2298,23 @@ void EditorExtension::deleteParagraph(KTextEditor::View 
*view)
 }
 
 // get the range of the current paragraph
-KTextEditor::Range 
EditorExtension::findCurrentParagraphRange(KTextEditor::View *view)
+KTextEditor::Range 
EditorExtension::findCurrentParagraphRange(KTextEditor::View* view, bool 
wholeLines)
 {
        view = determineView(view);
        if(!view) {
                return KTextEditor::Range::invalid();
        }
 
-       int startline, endline;
-       return (findCurrentTexParagraph(startline, endline, view))
-              ? KTextEditor::Range(startline, 0, endline + 1, 0)
-              : KTextEditor::Range::invalid();
+       int startline, endline, startcolumn, endcolumn;
+
+       if (findCurrentTexParagraph(startline, startcolumn, endline, endcolumn, 
view)) {
+               return wholeLines ?
+                       KTextEditor::Range(startline, 0, endline + 1, 0) :
+                       KTextEditor::Range(startline, startcolumn, endline, 
endcolumn);
+       }
+       else {
+                return KTextEditor::Range::invalid();
+       }
 }
 
 QString  EditorExtension::getParagraphText(KTextEditor::View *view)
@@ -2322,7 +2328,13 @@ QString  
EditorExtension::getParagraphText(KTextEditor::View *view)
        return (range.isValid()) ? view->document()->text(range) : QString();
 }
 
-bool EditorExtension::findCurrentTexParagraph(int &startline, int &endline, 
KTextEditor::View *view)
+bool EditorExtension::findCurrentTexParagraph(int& startline, int& endline, 
KTextEditor::View* view)
+{
+       int dummy;
+       return findCurrentTexParagraph(startline, dummy, endline, dummy, view);
+}
+
+bool EditorExtension::findCurrentTexParagraph(int& startline, int& 
startcolumn, int& endline, int& endcolumn, KTextEditor::View* view)
 {
        view = determineView(view);
        if(!view) {
@@ -2353,6 +2365,16 @@ bool EditorExtension::findCurrentTexParagraph(int 
&startline, int &endline, KTex
                startline = line;
        }
 
+       // it is guaranteed that 'startline.trimmed()' won't be empty
+       startcolumn = 0;
+       QString line = doc->line(startline);
+       for(int i = 0; i < line.size(); ++i) {
+               if(!line[i].isSpace()) {
+                       startcolumn = i;
+                       break;
+               }
+       }
+
        // find the next empty line
        for(int line = row + 1; line < doc->lines(); ++line) {
                if(doc->line(line).trimmed().isEmpty()) {
@@ -2361,6 +2383,16 @@ bool EditorExtension::findCurrentTexParagraph(int 
&startline, int &endline, KTex
                endline = line;
        }
 
+       // it is guaranteed that 'endline.trimmed()' won't be empty
+       line = doc->line(endline);
+       endcolumn = line.size();
+       for(int i = line.size() - 1; i >= 0; --i) {
+               if(!line[i].isSpace()) {
+                       endcolumn = i+1;
+                       break;
+               }
+       }
+
        // settings result
        return true;
 }
diff --git a/src/editorextension.h b/src/editorextension.h
index e963129..81d661b 100644
--- a/src/editorextension.h
+++ b/src/editorextension.h
@@ -85,7 +85,7 @@ public:
        KTextEditor::Range wordRange(const KTextEditor::Cursor &cursor, bool 
latexCommand=false, KTextEditor::View *view = NULL);
        QString word(const KTextEditor::Cursor &cursor, bool 
latexCommand=false, KTextEditor::View *view = NULL);
 
-       KTextEditor::Range findCurrentParagraphRange(KTextEditor::View *view);
+       KTextEditor::Range findCurrentParagraphRange(KTextEditor::View *view, 
bool wholeLines = true);
        QString getParagraphText(KTextEditor::View *view);
        int prevNonEmptyLine(int line, KTextEditor::View *view = NULL);
        int nextNonEmptyLine(int line, KTextEditor::View *view = NULL);
@@ -126,7 +126,7 @@ public Q_SLOTS:
        void matchTexgroup(KTextEditor::View *view = NULL);
        void closeTexgroup(KTextEditor::View *view = NULL);
 
-       void selectParagraph(KTextEditor::View *view = NULL);
+       void selectParagraph(KTextEditor::View *view = NULL, bool wholeLines = 
true);
        void selectLine(KTextEditor::View *view = NULL);
        void selectLines(int from, int to, KTextEditor::View *view = NULL);
        void selectWord(SelectMode mode = smTex, KTextEditor::View *view = 
NULL);
@@ -259,6 +259,7 @@ private:
 
        // find current paragraph
        bool findCurrentTexParagraph(int &startline, int &endline, 
KTextEditor::View *view);
+       bool findCurrentTexParagraph(int &startline, int& startcolumn, int 
&endline, int& endcolumn, KTextEditor::View *view);
 
        // sectioning commands
        bool findEndOfDocument(KTextEditor::Document *doc, int row, int col, 
int &rowFound, int &colFound);
diff --git a/src/scripting/kilescriptview.cpp b/src/scripting/kilescriptview.cpp
index bd875c7..0abdd77 100644
--- a/src/scripting/kilescriptview.cpp
+++ b/src/scripting/kilescriptview.cpp
@@ -193,12 +193,11 @@ void KileScriptView::selectMathgroup()
 
 ////////////////////////////////// Paragraph 
//////////////////////////////////////
 
-void KileScriptView::selectParagraph()
+void KileScriptView::selectParagraph(bool wholeLines)
 {
-       m_editor->selectParagraph(m_view);
+       m_editor->selectParagraph(m_view, wholeLines);
 }
 
-
 }
 
 #include "kilescriptview.moc"
diff --git a/src/scripting/kilescriptview.h b/src/scripting/kilescriptview.h
index b822c0f..9ddf8b8 100644
--- a/src/scripting/kilescriptview.h
+++ b/src/scripting/kilescriptview.h
@@ -87,7 +87,7 @@ class KileScriptView : public QObject, protected QScriptable
                Q_INVOKABLE void selectMathgroup();
 
                // paragraph
-               Q_INVOKABLE void selectParagraph();
+               Q_INVOKABLE void selectParagraph(bool wholeLines = true);
 
        private:
                KTextEditor::View *m_view;

Reply via email to