[kate] * backport to fix python-indentation-mode * remove extraneous requires

Than Ngo than at fedoraproject.org
Wed Mar 20 10:44:51 UTC 2013


commit 523c9d444ef3c38a0d12404ece1da2b0460a1cf1
Author: Than Ngo <than at redhat.com>
Date:   Wed Mar 20 11:43:16 2013 +0100

    * backport to fix python-indentation-mode
    * remove extraneous requires

 kate-4.10-python-indentation-mode.patch |  309 +++++++++++++++++++++++++++++++
 kate.spec                               |    7 +-
 2 files changed, 314 insertions(+), 2 deletions(-)
---
diff --git a/kate-4.10-python-indentation-mode.patch b/kate-4.10-python-indentation-mode.patch
new file mode 100644
index 0000000..f6b6622
--- /dev/null
+++ b/kate-4.10-python-indentation-mode.patch
@@ -0,0 +1,309 @@
+commit db18ba1a5791240094c975a71d9d09f74e39a07b
+Author: Gerald Senarclens de Grancy <oss at senarclens.eu>
+Date:   Wed Jan 30 13:36:13 2013 +0100
+
+    fixed and extended python indentation mode
+    
+    Indentation is now correct even if a comment is at the end of a line.
+    Added two regression tests.
+    
+    The updated version of the script also deals with opening and closing
+    argument lists and sequences as suggested in PEP8.
+    
+    BUG: 312309
+    REVIEW: 108644
+
+diff --git a/part/script/data/indentation/python.js b/part/script/data/indentation/python.js
+index 6ac14ec..0be69ec 100644
+--- a/part/script/data/indentation/python.js
++++ b/part/script/data/indentation/python.js
+@@ -1,114 +1,154 @@
+ /** kate-script
+  * name: Python
+  * license: LGPL
+- * author: Paul Giannaros <paul at giannaros.org>
+- * revision: 1
+- * kate-version: 3.4
++ * author: Paul Giannaros <paul at giannaros.org>, Gerald Senarclens de Grancy <oss at senarclens.eu>
++ * revision: 2
++ * kate-version: 3.10
+  */
+ 
++
+ // required katepart js libraries
+ require ("range.js");
++require ("string.js");
+ 
++openings = ['(', '[', '{'];
++closings = [')', ']', '}'];  // requires same order as in openings
++unindenters = ['continue', 'pass', 'raise', 'return']
+ 
+-// niceties
+ 
+-String.prototype.startsWith = function(prefix) {
+-    return this.substring(0, prefix.length) == prefix;
++// Return the given line without comments and leading or trailing whitespace.
++// Eg.
++// getCode(x) -> "for i in range(3):"
++//     if document.line(x) == "  for i in range(3):"
++// getCode(x) -> "for i in range(3):"
++//     if document.line(x) == "for i in range(3):  "
++// getCode(x) -> "for i in range(3):"
++//     if document.line(x) == "for i in range(3):  # grand"
++function getCode(lineNr) {
++    var line = document.line(lineNr);
++    var length = line.length;
++    while (length > 0 && document.isComment(lineNr, length - 1)) {
++        length--;
++    }
++    line = line.substr(0, length);  // strip the comment
++    return line.trim();
+ }
+ 
+-String.prototype.endsWith = function(suffix) {
+-    var startPos = this.length - suffix.length;
+-    if (startPos < 0)
+-        return false;
+-    return (this.lastIndexOf(suffix, startPos) == startPos);
+-}
+ 
+-String.prototype.lastCharacter = function() {
+-    var l = this.length;
+-    if(l == 0)
+-        return '';
+-    else
+-        return this.charAt(l - 1);
++// Return the indent if a opening bracket is not closed (incomplete sequence).
++// The calculated intent is the innermost opening bracket's position plus 1.
++// `lineNr`: the number of the line on which the brackets should be counted
++function _calcOpeningIndent(lineNr) {
++    var line = document.line(lineNr);
++    var countClosing = new Array();
++    closings.forEach(function(elem) {
++        countClosing[elem] = 0;
++    });
++    for (i = line.length - 1; i >= 0; --i) {
++        if (document.isComment(lineNr, i) || document.isString(lineNr, i))
++            continue;
++        if (closings.indexOf(line[i]) > -1)
++            countClosing[line[i]]++;
++        var index = openings.indexOf(line[i]);
++        if (index > -1) {
++            if (countClosing[closings[index]] == 0) {
++                return i + 1;
++            }
++            countClosing[closings[index]]--;
++        }
++    }
++    return -1;
+ }
+ 
+-String.prototype.sansWhiteSpace = function() {
+-    return this.replace(/[ \t\n\r]/g, '');
++
++// Return the indent if a closing bracket not opened (incomplete sequence).
++// The intent is the same as on the line with the unmatched opening bracket.
++// `lineNr`: the number of the line on which the brackets should be counted
++function _calcClosingIndent(lineNr) {
++    var line = document.line(lineNr);
++    var countClosing = new Array();
++    closings.forEach(function(elem) {
++        countClosing[elem] = 0;
++    });
++    for (i = line.length - 1; i >= 0; --i) {
++        if (document.isComment(lineNr, i) || document.isString(lineNr, i))
++            continue;
++        if (closings.indexOf(line[i]) > -1)
++            countClosing[line[i]]++;
++        var index = openings.indexOf(line[i]);
++        if (index > -1)
++            countClosing[closings[index]]--;
++    }
++    for (var key in countClosing) {
++        if (countClosing[key] > 0) {  // unmatched closing bracket
++            for (--lineNr; lineNr >= 0; --lineNr) {
++                if (_calcOpeningIndent(lineNr) > -1)
++                    return document.firstVirtualColumn(lineNr)
++            }
++        }
++    }
++    return -1;
+ }
+-String.prototype.stripWhiteSpace = function() {
+-    return this.replace(/^[ \t\n\r]+/, '').replace(/[ \t\n\r]+$/, '');
++
++
++// Returns the indent for mismatched (opening or closing) brackets.
++// If there are no mismatched brackets, -1 is returned.
++// `lineNr`: number of the line for which the indent is calculated
++function calcBracketIndent(lineNr) {
++    var indent = _calcOpeningIndent(lineNr - 1);
++    if (indent > -1)
++        return indent
++    indent = _calcClosingIndent(lineNr - 1);
++    if (indent > -1)
++        return indent
++    return -1;
+ }
+ 
+ 
+-function dbg(s) {
+-    // debug to the term in blue so that it's easier to make out amongst all
+-    // of Kate's other debug output.
+-    debug("\u001B[34m" + s + "\u001B[0m");
++// Return true if a single unindent should occur.
++function shouldUnindent(LineNr) {
++    lastLine = getCode(LineNr - 1);
++    if (unindenters.indexOf(lastLine) >= 0 || lastLine.startsWith('raise ') ||
++        lastLine.startsWith('return '))
++        return 1;
++    // unindent if the last line was indented b/c of a backslash
++    if (LineNr >= 2) {
++        secondLastLine = getCode(LineNr - 2);
++        if (secondLastLine.length && secondLastLine.substr(-1) == "\\")
++            return 1;
++    }
++    return 0;
+ }
+ 
+-var triggerCharacters = "";
+ 
+-// General notes:
+-// indent() returns the amount of characters (in spaces) to be indented.
++// Return the amount of characters (in spaces) to be indented.
+ // Special indent() return values:
+ //   -2 = no indent
+ //   -1 = keep last indent
+-
++// Follow PEP8 for unfinished sequences and argument lists.
++// Nested sequences are not implemented. (neither by Emacs' python-mode)
+ function indent(line, indentWidth, character) {
+-//     dbg(document.attribute.toString());
+-//     dbg("indent character: " + character);
+-//     dbg("line text: " + document.line(line));
+-    var currentLine = document.line(line);
+-//     dbg("current line: " + currentLine);
+-    var lastLine = document.line(line - 1);
+-    var lastCharacter = lastLine.lastCharacter();
+-    // we can't really indent line 0
+-    if(line == 0)
++    if (line == 0)  // don't ever act on document's first line
+         return -2;
+-    if(lastLine == "")
++    if (!document.line(line - 1).length)  // empty line
+         return -2;
+-    // make sure the last line is code
+-    if(!document.isCode(line - 1, document.lineLength(line - 1) - 1) && lastCharacter != "\"" && lastCharacter != "'") {
+-//         dbg("attributes that we don't want! Returning");
+-        return -1;
+-    }
+-    // otherwise, check the line contents
+-    // for :, we simply indent
+-//     dbg('line without white space: ' + currentLine.sansWhiteSpace().length);
+-    if(lastLine.endsWith(':')) {
+-//         dbg('indenting line for :');
+-        return document.firstVirtualColumn(line - 1) + indentWidth;
+-    }
+-    // generally, when a brace is on its own at the end of a regular line
+-    // (i.e a data structure is being started), indent is wanted.
+-    // For example:
+-    // dictionary = {
+-    //     'foo': 'bar',
+-    // }
+-    // etc..
+-    else if(lastCharacter == '{' || lastCharacter == '[') {
+-//         dbg('indenting for { or [');
++    var lastLine = getCode(line - 1);
++    var lastChar = lastLine.substr(-1);
++
++    // indent when opening bracket or backslash is at the end the previous line
++    if (openings.indexOf(lastChar) >= 0 || lastChar == "\\") {
+         return document.firstVirtualColumn(line - 1) + indentWidth;
+     }
+-    // XX this introduces irritating bugs. Commenting it out for now
+-    //
+-//     // relatively simplistic heuristic for determining whether or not to unindent:
+-//     // if a }] has been typed and it's the sole letter on the line, unindent
+-//     else if((character == '}' || character == ']') && (currentLine.sansWhiteSpace().length == 1)) {
+-//         dbg('unindenting line for } or ]');
+-//         return Math.max(0, document.firstVirtualColumn(line - 1) - indentWidth);
+-//     }
+-    // finally, a raise, pass, and continue should unindent
+-    lastLine = lastLine.stripWhiteSpace();
+-    if(    lastLine == 'continue' || lastLine == 'pass' || lastLine == 'raise' || lastLine.startsWith('raise ')
+-        || lastLine.startsWith('return ') || lastLine == 'return'
+-    )
+-    {
+-//         dbg('unindenting line for keyword');
++    // continue, pass, raise, return etc. should unindent
++    if (shouldUnindent(line))
+         return Math.max(0, document.firstVirtualColumn(line - 1) - indentWidth);
++    var indent = calcBracketIndent(line);
++    if (lastLine.endsWith(':')) {
++        if (indent > -1)
++            return indent + indentWidth;
++        return document.firstVirtualColumn(line - 1) + indentWidth;
+     }
+-//     dbg('continuing with regular indent');
+-    return -1;
++    return indent;
+ }
+ 
+-
+ // kate: space-indent on; indent-width 4; replace-tabs on;
+diff --git a/testdata/indent/python/dedentComment/expected b/testdata/indent/python/dedentComment/expected
+new file mode 100644
+index 0000000..619fd39
+--- /dev/null
++++ b/testdata/indent/python/dedentComment/expected
+@@ -0,0 +1,3 @@
++def some_function():
++    return  # comment
++pass
+\ No newline at end of file
+diff --git a/testdata/indent/python/dedentComment/input.js b/testdata/indent/python/dedentComment/input.js
+new file mode 100644
+index 0000000..c0a8574
+--- /dev/null
++++ b/testdata/indent/python/dedentComment/input.js
+@@ -0,0 +1,3 @@
++v.setCursorPosition(1, document.lineLength(1));
++v.enter();
++v.type("pass");
+\ No newline at end of file
+diff --git a/testdata/indent/python/dedentComment/origin b/testdata/indent/python/dedentComment/origin
+new file mode 100644
+index 0000000..4c8ee38
+--- /dev/null
++++ b/testdata/indent/python/dedentComment/origin
+@@ -0,0 +1,2 @@
++def some_function():
++    return  # comment
+\ No newline at end of file
+diff --git a/testdata/indent/python/indentComment/expected b/testdata/indent/python/indentComment/expected
+new file mode 100644
+index 0000000..8168a1b
+--- /dev/null
++++ b/testdata/indent/python/indentComment/expected
+@@ -0,0 +1,2 @@
++def some_function():  # comment
++    pass
+\ No newline at end of file
+diff --git a/testdata/indent/python/indentComment/input.js b/testdata/indent/python/indentComment/input.js
+new file mode 100644
+index 0000000..9d13b64
+--- /dev/null
++++ b/testdata/indent/python/indentComment/input.js
+@@ -0,0 +1,3 @@
++v.setCursorPosition(0, document.lineLength(0));
++v.enter();
++v.type("pass");
+\ No newline at end of file
+diff --git a/testdata/indent/python/indentComment/origin b/testdata/indent/python/indentComment/origin
+new file mode 100644
+index 0000000..cc027e0
+--- /dev/null
++++ b/testdata/indent/python/indentComment/origin
+@@ -0,0 +1 @@
++def some_function():  # comment
+\ No newline at end of file
diff --git a/kate.spec b/kate.spec
index 5091b5f..f09f661 100644
--- a/kate.spec
+++ b/kate.spec
@@ -17,6 +17,9 @@ URL:     https://projects.kde.org/projects/kde/kdebase/kate
 %endif
 Source0: http://download.kde.org/%{stable}/%{version}/src/%{name}-%{version}.tar.xz
 
+# upstream patches
+Patch100: kate-4.10-python-indentation-mode.patch
+
 BuildRequires: desktop-file-utils
 BuildRequires: kactivities-devel >= %{version}
 BuildRequires: kdelibs4-devel >= %{version}
@@ -35,7 +38,6 @@ Requires: %{name}-libs%{?_isa} = %{version}-%{release}
 %package devel
 Summary:  Development files for %{name}
 Requires: %{name}-libs%{?_isa} = %{version}-%{release}
-Requires: %{name}-part = %{version}-%{release}
 Requires: kdelibs4-devel
 %description devel
 %{summary}.
@@ -72,6 +74,7 @@ Requires: kde-runtime%{?_kde4_version: >= %{_kde4_version}}
 %prep
 %setup -q
 
+%patch100 -p1 -b .python-indentation-mode
 
 %build
 mkdir -p %{_target_platform}
@@ -194,7 +197,7 @@ fi
 
 %changelog
 * Tue Mar 19 2013 Than Ngo <than at redhat.com> - 4.10.1-3
-- add missing requires on kate-part to kate-devel
+- backport to fix python indentation mode
 
 * Tue Mar 19 2013 Than Ngo <than at redhat.com> - 4.10.1-2
 - Fix documentation multilib conflict in index.cache


More information about the scm-commits mailing list