-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler_impl_sample.py
217 lines (188 loc) · 8.72 KB
/
handler_impl_sample.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import re
from typing import Dict, List, Optional
from overrides import overrides
from server.protocol import (
CodeAction, Command, CompletionItem, CompletionList, Diagnostic, DiagnosticSeverity, Example,
Hover, Location, Position, Range, SyntaxHighlight, TextEdit, WorkspaceEdit
)
from server.teaspn_handler import TeaspnHandler
class TeaspnHandlerSample(TeaspnHandler):
"""
A sample TeaspnHandler implementation with simple writing assistance features.
"""
def __init__(self):
# Manages a mapping from Diagnostic to replacements
self._diag_to_replacements: Dict[Diagnostic, List[str]] = {}
super().__init__()
@overrides
def highlight_syntax(self) -> List[SyntaxHighlight]:
"""
Implements sample syntax highlighting where all occurrences of CAPS WORDS and numbers
are highlighted in different colors with hover messages.
"""
highlights = []
for line_id, line_text in enumerate(self._text.splitlines()):
for match in re.finditer(r'\b[A-Z]+\b', line_text):
rng = Range(start=Position(line=line_id, character=match.start()),
end=Position(line=line_id, character=match.end()))
highlights.append(SyntaxHighlight(range=rng,
type='blue',
hoverMessage=f'ALL CAPS: {match.group(0)}'))
for match in re.finditer(r'\b[0-9]+\b', line_text):
rng = Range(start=Position(line=line_id, character=match.start()),
end=Position(line=line_id, character=match.end()))
highlights.append(SyntaxHighlight(range=rng,
type='red',
hoverMessage=f'numbers: {match.group(0)}'))
return highlights
@overrides
def get_diagnostics(self) -> List[Diagnostic]:
"""
Implements sample grammatical error detection (GED) and correction (GEC).
This checks English subject-verb agreement in statement sentences using hand-crafted rules.
"""
PRONOUNS = ['i', 'you', 'we', 'she', 'he', 'they', 'it']
BE_VERBS = ['am', 'are', 'is', 'were', 'was']
VALID_COMBINATIONS = {
'i': {'am', 'was'},
'you': {'are', 'were'},
'we': {'are', 'were'},
'she': {'is', 'was'},
'he': {'is', 'was'},
'they': {'are', 'were'},
'it': {'is', 'was'}
}
diagnostics = []
for line_id, line_text in enumerate(self._text.splitlines()):
matches = list(re.finditer(r'\w+', line_text))
for m1, m2 in zip(matches, matches[1:]):
w1, w2 = m1.group(0), m2.group(0)
if not (w1.lower() in PRONOUNS and w2.lower() in BE_VERBS):
continue
if w2.lower() in VALID_COMBINATIONS[w1.lower()]:
continue
rng = Range(start=Position(line=line_id, character=m1.start(0)),
end=Position(line=line_id, character=m2.end(0)))
diagnostic = Diagnostic(range=rng,
severity=DiagnosticSeverity.Error,
message='Wrong agreement: {} {}'.format(w1, w2))
diagnostics.append(diagnostic)
# Register the diagnostic for later use in GEC
replacements = [f'{w1} {verb}' for verb in VALID_COMBINATIONS[w1.lower()]]
self._diag_to_replacements[diagnostic] = replacements
return diagnostics
@overrides
def run_quick_fix(self, range: Range, diagnostics: List[Diagnostic]) -> List[CodeAction]:
"""
Implements GEC for fixing subject-verb agreement errors detected above.
"""
actions = []
for diag in diagnostics:
for repl in self._diag_to_replacements[diag]:
edit = WorkspaceEdit({self._uri: [TextEdit(range=diag.range, newText=repl)]})
command = Command(title='Quick fix: {}'.format(repl),
command='refactor.rewrite',
arguments=[edit])
actions.append(CodeAction(title='Quick fix: {}'.format(repl),
kind='quickfix',
command=command))
return actions
@overrides
def run_code_action(self, range: Range) -> List[Command]:
"""
Implements sample text rewriting where uppercase and lowercase rewriting is provided.
"""
target_text = self._get_text(range)
if not target_text:
return []
text_upper = target_text.upper()
edit_upper = WorkspaceEdit({self._uri: [TextEdit(range=range, newText=text_upper)]})
command_upper = Command(title=f'UPPER: {text_upper}',
command='refactor.rewrite',
arguments=[edit_upper])
text_lower = target_text.lower()
edit_lower = WorkspaceEdit({self._uri: [TextEdit(range=range, newText=text_lower)]})
command_lower = Command(title=f'lower: {text_lower}',
command='refactor.rewrite',
arguments=[edit_lower])
return [command_upper, command_lower]
@overrides
def search_example(self, query: str) -> List[Example]:
"""
Sample search feature which returns a list of tea varieties and their descriptions,
regardless of the query.
"""
return [
Example(label="Assam",
description="a black tea named after the region of its production, "
"Assam, India."),
Example(label="Chai",
description="a flavoured tea beverage made by brewing black tea "
"with a mixture of aromatic spices and herbs."),
Example(label="Darjeeling",
description="a tea grown in the Darjeeling district, Kalimpong District "
"in West Bengal, India, and widely exported and known."),
Example(label="Earl Grey",
description="a tea blend which has been flavoured "
"with the addition of oil of bergamot."),
Example(label="Jasmine",
description="tea scented with the aroma of jasmine blossoms."),
Example(label="Matcha",
description="finely ground powder of specially grown "
"and processed green tea leaves."),
Example(label="Oolong",
description="a traditional semi-oxidized Chinese tea"),
Example(label="Puerh",
description="a variety of fermented tea produced "
"in the Yunnan province of China.")
]
@overrides
def search_definition(self, position: Position, uri: str) -> List[Location]:
"""
Implements a "go to definition" feature where all occurrence of the target word are returned
"""
word = self._get_word_at(position)
locations = []
offset = 0
while True:
offset = self._text.find(word, offset)
if offset == -1:
break
rng = Range(start=self._offset_to_position(offset),
end=self._offset_to_position(offset + len(word)))
locations.append(Location(uri=uri, range=rng))
offset += 1
return locations
@overrides
def get_completion_list(self, position: Position) -> CompletionList:
"""
Implements simple word completion with tea variety names
"""
COMPLETION_WORDS = [
"Assam",
"Chai",
"Darjeeling",
"Earl Grey",
"Jasmine",
"Matcha",
"Oolong",
"Puerh"
]
offset = self._position_to_offset(position)
context = self._text[:offset]
query = context.split()[-1]
items = []
for word in COMPLETION_WORDS:
if word.startswith(query):
items.append(CompletionItem(label=word))
return CompletionList(isIncomplete=True, items=items)
@overrides
def hover(self, position: Position) -> Optional[Hover]:
"""
Implements a sample hover feature where a massage "word: {identity of word}" is shown
when the cursor hovers over a word.
"""
word = self._get_word_at(position)
if not word:
return None
return Hover(contents=f'word: {word}')