~ubuntu-branches/ubuntu/vivid/kate/vivid-updates

« back to all changes in this revision

Viewing changes to addons/kate/pate/src/plugins/libkatepate/errors.py

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2014-12-04 16:49:41 UTC
  • mfrom: (1.6.6)
  • Revision ID: package-import@ubuntu.com-20141204164941-l3qbvsly83hhlw2v
Tags: 4:14.11.97-0ubuntu1
* New upstream release
* Update build-deps and use pkg-kde v3 for Qt 5 build
* kate-data now kate5-data for co-installability

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: utf-8 -*-
2
 
# Copyright (c) 2013 by Pablo Martín <goinnn@gmail.com>
3
 
#
4
 
# This software is free software: you can redistribute it and/or modify
5
 
# it under the terms of the GNU Lesser General Public License as published by
6
 
# the Free Software Foundation, either version 3 of the License, or
7
 
# (at your option) any later version.
8
 
#
9
 
# This software is distributed in the hope that it will be useful,
10
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
# GNU Lesser General Public License for more details.
13
 
#
14
 
# You should have received a copy of the GNU Lesser General Public License
15
 
# along with this software.  If not, see <http://www.gnu.org/licenses/>.
16
 
 
17
 
# This core originally was in this repository:
18
 
# <https://github.com/goinnn/Kate-plugins/blob/master/kate_plugins/pyte_plugins/check_plugins/commons.py>
19
 
 
20
 
import kate
21
 
import kate.ui
22
 
 
23
 
import sys
24
 
 
25
 
from PyKDE4.kdecore import i18n, i18nc
26
 
from PyKDE4.ktexteditor import KTextEditor
27
 
 
28
 
 
29
 
ENCODING_TRANSLATIONS = 'latin-1'
30
 
 
31
 
 
32
 
def clearMarksOfError(doc, mark_iface):
33
 
    for line in range(doc.lines()):
34
 
        if mark_iface.mark(line) == mark_iface.Error:
35
 
            mark_iface.removeMark(line, mark_iface.Error)
36
 
 
37
 
 
38
 
def showErrors(message, errors, key_mark, doc, icon='dialog-warning',
39
 
               key_line='line', key_column='column',
40
 
               max_errors=3, show_popup=True, move_cursor=False):
41
 
    mark_iface = doc.markInterface()
42
 
    messages = {}
43
 
    view = kate.activeView()
44
 
    pos = view.cursorPosition()
45
 
    current_line = pos.line() + 1
46
 
    current_column = pos.column() + 1
47
 
    for error in errors:
48
 
        header = False
49
 
        line = error[key_line]
50
 
        column = error.get(key_column, 0)
51
 
        pos = _compress_key(line, column)
52
 
        if not messages.get(line, None):
53
 
            header = True
54
 
            messages[pos] = []
55
 
        error_message = _generateErrorMessage(error, key_line, key_column, header)
56
 
        messages[pos].append(error_message)
57
 
        mark_iface.setMark(line - 1, mark_iface.Error)
58
 
 
59
 
    messages_items = list(messages.items())  # Python 3 compatible
60
 
    messages_items.sort()
61
 
    if move_cursor:
62
 
        first_error, messages_show = _getErrorMessagesOrder(messages_items,
63
 
                                                            max_errors,
64
 
                                                            current_line,
65
 
                                                            current_column)
66
 
        line_to_move, column_to_move = _uncompress_key(messages_items[first_error][0])
67
 
        _moveCursorTFirstError(line_to_move, column_to_move)
68
 
    else:
69
 
        first_error, messages_show = _getErrorMessagesOrder(messages_items, max_errors)
70
 
    if show_popup:
71
 
        message = '%s\n%s' % (message, '\n'.join(messages_show))
72
 
        if len(messages_show) < len(errors):
73
 
            message += i18n('\n\nAnd other errors')
74
 
        showError(message, icon)
75
 
 
76
 
 
77
 
def showError(message="error", icon="dialog-warning"):
78
 
    kate.ui.popup(i18nc('@title:window', 'Error'), message, icon)
79
 
 
80
 
 
81
 
def showOk(message="Ok", icon='dialog-ok'):
82
 
    kate.ui.popup(i18nc('@title:window', 'Success'), message, icon)
83
 
 
84
 
 
85
 
def _compress_key(line, column):
86
 
    doc = kate.activeDocument()
87
 
    cipher = len('%s' % doc.lines())
88
 
    key_template = '%%%sd' % cipher
89
 
    key_template += '__%3d'
90
 
    return key_template % (line, column)
91
 
 
92
 
 
93
 
def _uncompress_key(key):
94
 
    line, column = key.split('__')
95
 
    return (int(line), int(column))
96
 
 
97
 
 
98
 
def _generateErrorMessage(error, key_line='line', key_column='column', header=True):
99
 
    message = ''
100
 
    exclude_keys = [key_line, key_column, 'filename']
101
 
    line = error[key_line]
102
 
    column = error.get(key_column, None)
103
 
    if header or column:
104
 
        column = error.get(key_column, None)
105
 
        if column:
106
 
            message = i18n('~*~ Position: (%1, %2)', line, column)
107
 
        else:
108
 
            message = i18n('~*~ Line: %1', line)
109
 
        message += ' ~*~'
110
 
    for key, value in error.items():
111
 
        if value and key not in exclude_keys:
112
 
            if key != 'message':
113
 
                message = '%s\n     * %s: %s' % (message, key, value)
114
 
            else:
115
 
                message = '%s\n     %s' % (message, value)
116
 
    return message
117
 
 
118
 
 
119
 
def _getErrorMessagesOrder(messages_items, max_errors, current_line=None, current_column=None):
120
 
    messages_order = []
121
 
    first_error = None
122
 
    num_messages = 0
123
 
    if not current_line:
124
 
        for line, message in messages_items:
125
 
            messages_order.extend(message)
126
 
            num_messages += 1
127
 
            if num_messages >= max_errors:
128
 
                break
129
 
        return (0, messages_order)
130
 
    for i, error in enumerate(messages_items):
131
 
        line, column = _uncompress_key(error[0])
132
 
        message = error[1]
133
 
        if line > current_line or (line == current_line and column > current_column):
134
 
            if first_error is None:
135
 
                first_error = i
136
 
            num_messages += 1
137
 
            messages_order.extend(message)
138
 
        if num_messages >= max_errors:
139
 
            break
140
 
    if len(messages_order) == max_errors:
141
 
        return (first_error, messages_order)
142
 
    for i, error in enumerate(messages_items):
143
 
        line, column = _uncompress_key(error[0])
144
 
        message = error[1]
145
 
        if line <= current_line:
146
 
            if first_error is None:
147
 
                first_error = i
148
 
            num_messages += 1
149
 
            messages_order.extend(message)
150
 
        else:
151
 
            break
152
 
        if num_messages >= max_errors:
153
 
            break
154
 
    return (first_error, messages_order)
155
 
 
156
 
 
157
 
def _moveCursorTFirstError(line, column=None):
158
 
    try:
159
 
        column = column or 0
160
 
        cursor = KTextEditor.Cursor(line - 1, column - 1)
161
 
        view = kate.activeView()
162
 
        view.setCursorPosition(cursor)
163
 
    except KeyError:
164
 
        pass