Skip to content

Commit e90810e

Browse files
committed
feat: adding a line/column indication in the command line feedback, patching the suggestions print
1 parent 164d14f commit e90810e

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

language_tool_python/__main__.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,12 @@ def main() -> int:
200200

201201
# Messages that end with punctuation already include the
202202
# suggestion.
203-
if replacement_text and not message.endswith(('.', '?')):
204-
message += '; suggestions: ' + replacement_text
203+
if replacement_text and not message.endswith('?'):
204+
message += ' Suggestions: ' + replacement_text
205+
206+
line, column = match.get_line_and_column(text)
205207

206-
print(f'{filename}: {rule_id}: {message}')
208+
print(f'{filename}:{line}:{column}: {rule_id}: {message}')
207209

208210
status = 2
209211
except LanguageToolError as exception:

language_tool_python/match.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import unicodedata
22
from collections import OrderedDict
3-
from typing import Any, Dict, Iterator, OrderedDict as OrderedDictType
3+
from typing import Any, Dict, Tuple, Iterator, OrderedDict as OrderedDictType
44
from functools import total_ordering
55

66
def get_match_ordered_dict() -> OrderedDictType[str, type]:
@@ -185,6 +185,23 @@ def matchedText(self) -> str:
185185
:rtype: str
186186
"""
187187
return self.context[self.offsetInContext:self.offsetInContext+self.errorLength]
188+
189+
def get_line_and_column(self, original_text: str) -> Tuple[int, int]:
190+
"""
191+
Returns the line and column number of the error in the context.
192+
193+
:param original_text: The original text in which the error occurred. We need this to calculate the line and column number, because the context has no more newline characters.
194+
:type original_text: str
195+
:return: A tuple containing the line and column number of the error.
196+
:rtype: Tuple[int, int]
197+
"""
198+
199+
context_without_additions = self.context[3:-3] if len(self.context) > 6 else self.context
200+
if context_without_additions not in original_text.replace('\n', ' '):
201+
raise ValueError('The original text does not match the context of the error')
202+
line = original_text.count('\n', 0, self.offset)
203+
column = self.offset - original_text.rfind('\n', 0, self.offset)
204+
return line + 1, column
188205

189206
def select_replacement(self, index: int) -> None:
190207
"""

0 commit comments

Comments
 (0)