1
# -*- coding: utf-8 -*-
2
# Copyright (c) 2013 by Pablo Martín <goinnn@gmail.com>
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.
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.
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/>.
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>
25
from PyKDE4.kdecore import i18n, i18nc
26
from PyKDE4.ktexteditor import KTextEditor
29
ENCODING_TRANSLATIONS = 'latin-1'
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)
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()
43
view = kate.activeView()
44
pos = view.cursorPosition()
45
current_line = pos.line() + 1
46
current_column = pos.column() + 1
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):
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)
59
messages_items = list(messages.items()) # Python 3 compatible
62
first_error, messages_show = _getErrorMessagesOrder(messages_items,
66
line_to_move, column_to_move = _uncompress_key(messages_items[first_error][0])
67
_moveCursorTFirstError(line_to_move, column_to_move)
69
first_error, messages_show = _getErrorMessagesOrder(messages_items, max_errors)
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)
77
def showError(message="error", icon="dialog-warning"):
78
kate.ui.popup(i18nc('@title:window', 'Error'), message, icon)
81
def showOk(message="Ok", icon='dialog-ok'):
82
kate.ui.popup(i18nc('@title:window', 'Success'), message, icon)
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)
93
def _uncompress_key(key):
94
line, column = key.split('__')
95
return (int(line), int(column))
98
def _generateErrorMessage(error, key_line='line', key_column='column', header=True):
100
exclude_keys = [key_line, key_column, 'filename']
101
line = error[key_line]
102
column = error.get(key_column, None)
104
column = error.get(key_column, None)
106
message = i18n('~*~ Position: (%1, %2)', line, column)
108
message = i18n('~*~ Line: %1', line)
110
for key, value in error.items():
111
if value and key not in exclude_keys:
113
message = '%s\n * %s: %s' % (message, key, value)
115
message = '%s\n %s' % (message, value)
119
def _getErrorMessagesOrder(messages_items, max_errors, current_line=None, current_column=None):
124
for line, message in messages_items:
125
messages_order.extend(message)
127
if num_messages >= max_errors:
129
return (0, messages_order)
130
for i, error in enumerate(messages_items):
131
line, column = _uncompress_key(error[0])
133
if line > current_line or (line == current_line and column > current_column):
134
if first_error is None:
137
messages_order.extend(message)
138
if num_messages >= max_errors:
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])
145
if line <= current_line:
146
if first_error is None:
149
messages_order.extend(message)
152
if num_messages >= max_errors:
154
return (first_error, messages_order)
157
def _moveCursorTFirstError(line, column=None):
160
cursor = KTextEditor.Cursor(line - 1, column - 1)
161
view = kate.activeView()
162
view.setCursorPosition(cursor)