Skip to content

Commit 311bc72

Browse files
committed
Support offset attribute. Remove frompos and topos.
1 parent 1e06654 commit 311bc72

1 file changed

Lines changed: 11 additions & 41 deletions

File tree

‎language_tool/__init__.py‎

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -92,18 +92,16 @@ class Match:
9292
"""
9393
_SLOTS = OrderedDict([
9494
("fromy", int), ("fromx", int), ("toy", int), ("tox", int),
95-
("frompos", int), ("topos", int),
9695
("ruleId", str), ("subId", str), ("msg", str),
9796
("replacements", get_replacement_list),
98-
("context", str), ("contextoffset", int), ("errorlength", int),
97+
("context", str), ("contextoffset", int),
98+
("offset", int), ("errorlength", int),
9999
("url", str), ("category", str),
100100
])
101-
_frompos_cache, _topos_cache = None, None
102101

103102
def __init__(self, attrib, text=None):
104103
for k, v in attrib.items():
105104
setattr(self, k, v)
106-
self._lines = text
107105

108106
def __repr__(self):
109107
def _ordered_dict_repr():
@@ -151,39 +149,9 @@ def __setattr__(self, name, value):
151149
super().__setattr__(name, value)
152150

153151
def __getattr__(self, name):
154-
# Fallback to calculated `frompos` and `topos` attributes
155-
# if using unpatched LanguageTool server.
156-
if name == "frompos":
157-
return self._frompos
158-
elif name == "topos":
159-
return self._topos
160-
elif name not in self._SLOTS:
161-
raise AttributeError(
162-
"{!r} object has no attribute {!r}"
163-
.format(self.__class__.__name__, name)
164-
)
165-
166-
@property
167-
def _frompos(self):
168-
"""Position of the start of the error
169-
"""
170-
if self._frompos_cache is None:
171-
self._frompos_cache = self._get_pos(self.fromy, self.fromx)
172-
return self._frompos_cache
173-
174-
@property
175-
def _topos(self):
176-
"""Position of the end of the error
177-
"""
178-
if self._topos_cache is None:
179-
self._topos_cache = self._get_pos(self.toy, self.tox)
180-
return self._topos_cache
181-
182-
def _get_pos(self, y, x):
183-
if not isinstance(self._lines, list):
184-
self._lines = self._lines.split("\n")
185-
prev_lines = self._lines[:y]
186-
return sum([len(line) for line in prev_lines]) + len(prev_lines) + x
152+
if name not in self._SLOTS:
153+
raise AttributeError("{!r} object has no attribute {!r}"
154+
.format(self.__class__.__name__, name))
187155

188156

189157
class LanguageTool:
@@ -447,15 +415,17 @@ def correct(text: str, matches: [Match]) -> str:
447415
"""
448416
ltext = list(text)
449417
matches = [match for match in matches if match.replacements]
450-
errors = [ltext[match.frompos:match.topos] for match in matches]
451-
offset = 0
418+
errors = [ltext[match.offset:match.offset+match.errorlength]
419+
for match in matches]
420+
correct_offset = 0
452421
for n, match in enumerate(matches):
453-
frompos, topos = match.frompos + offset, match.topos + offset
422+
frompos, topos = (correct_offset + match.offset,
423+
correct_offset + match.offset + match.errorlength)
454424
if ltext[frompos:topos] != errors[n]:
455425
continue
456426
repl = match.replacements[0]
457427
ltext[frompos:topos] = list(repl)
458-
offset += len(repl) - len(errors[n])
428+
correct_offset += len(repl) - len(errors[n])
459429
return "".join(ltext)
460430

461431

0 commit comments

Comments
 (0)