1 | <?php |
||
2 | /** |
||
3 | * Twigfield for Craft CMS |
||
4 | * |
||
5 | * Provides a twig editor field with Twig & Craft API autocomplete |
||
6 | * |
||
7 | * @link https://nystudio107.com |
||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||
8 | * @copyright Copyright (c) 2022 nystudio107 |
||
0 ignored issues
–
show
|
|||
9 | */ |
||
0 ignored issues
–
show
|
|||
10 | |||
11 | namespace nystudio107\twigfield\models; |
||
12 | |||
13 | use craft\base\Model; |
||
14 | use craft\validators\ArrayValidator; |
||
15 | use nystudio107\twigfield\base\AutocompleteInterface; |
||
16 | use nystudio107\twigfield\types\CompleteItemKind; |
||
17 | |||
18 | /** |
||
19 | * Based on: https://microsoft.github.io/monaco-editor/api/interfaces/monaco.languages.CompletionItem.html |
||
20 | * |
||
21 | * @author nystudio107 |
||
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
![]() |
|||
22 | * @package Twigfield |
||
0 ignored issues
–
show
|
|||
23 | * @since 1.0.11 |
||
0 ignored issues
–
show
|
|||
24 | */ |
||
0 ignored issues
–
show
|
|||
25 | class CompleteItem extends Model |
||
26 | { |
||
27 | // Public Static Methods |
||
28 | // ========================================================================= |
||
29 | |||
30 | /** |
||
31 | * Factory method for complete item objects |
||
32 | * |
||
33 | * @return CompleteItem |
||
34 | */ |
||
35 | public static function create(): CompleteItem |
||
36 | { |
||
37 | return new CompleteItem(); |
||
38 | } |
||
39 | |||
40 | // Public Properties |
||
41 | // ========================================================================= |
||
42 | |||
43 | /** |
||
0 ignored issues
–
show
|
|||
44 | * @var array An optional array of additional text edits that are applied when selecting this completion. |
||
45 | * Edits must not overlap with the main edit nor with themselves. |
||
46 | * ref: https://microsoft.github.io/monaco-editor/api/interfaces/monaco.editor.ISingleEditOperation.html |
||
47 | */ |
||
48 | public $additionalTextEdits; |
||
49 | |||
50 | /** |
||
0 ignored issues
–
show
|
|||
51 | * @var array A command that should be run upon acceptance of this item. |
||
52 | * ref: https://microsoft.github.io/monaco-editor/api/interfaces/monaco.languages.Command.html |
||
53 | */ |
||
54 | public $command; |
||
55 | |||
56 | /** |
||
0 ignored issues
–
show
|
|||
57 | * @var array An optional set of characters that when pressed while this completion is active will accept |
||
58 | * it first and then type that character. Note that all commit characters should have `length=1` and that |
||
59 | * superfluous characters will be ignored. |
||
60 | */ |
||
61 | public $commitCharacters; |
||
62 | |||
63 | /** |
||
0 ignored issues
–
show
|
|||
64 | * @var string A human-readable string with additional information about this item, like type or symbol information. |
||
65 | */ |
||
66 | public $detail; |
||
67 | |||
68 | /** |
||
0 ignored issues
–
show
|
|||
69 | * @var string A human-readable string that represents a doc-comment. |
||
70 | * Can contain Markdown |
||
71 | */ |
||
72 | public $documentation; |
||
73 | |||
74 | /** |
||
0 ignored issues
–
show
|
|||
75 | * @var string A string that should be used when filtering a set of completion items. |
||
76 | * When falsy the `label` is used. |
||
77 | */ |
||
78 | public $filterText; |
||
79 | |||
80 | /** |
||
0 ignored issues
–
show
|
|||
81 | * @var string A string or snippet that should be inserted in a document when selecting this completion. |
||
82 | */ |
||
83 | public $insertText = ''; |
||
84 | |||
85 | /** |
||
0 ignored issues
–
show
|
|||
86 | * @var int Additional rules (as bitmask) that should be applied when inserting this completion. |
||
87 | */ |
||
88 | public $insertTextRules; |
||
89 | |||
90 | /** |
||
0 ignored issues
–
show
|
|||
91 | * @var int The kind of this completion item. Based on the kind an icon is chosen by the editor. |
||
92 | */ |
||
93 | public $kind = CompleteItemKind::PropertyKind; |
||
94 | |||
95 | /** |
||
0 ignored issues
–
show
|
|||
96 | * @var string The label of this completion item. By default this is also the text that is inserted |
||
97 | * when selecting this completion. |
||
98 | */ |
||
99 | public $label; |
||
100 | |||
101 | /** |
||
0 ignored issues
–
show
|
|||
102 | * @var bool Select this item when showing. Note that only one completion item can be selected and that |
||
103 | * the editor decides which item that is. The rule is that the first item of those that match best is selected. |
||
104 | */ |
||
105 | public $preselect; |
||
106 | |||
107 | /** |
||
0 ignored issues
–
show
|
|||
108 | * @var array A range of text that should be replaced by this completion item. |
||
109 | */ |
||
110 | public $range; |
||
111 | |||
112 | /** |
||
0 ignored issues
–
show
|
|||
113 | * @var string A string that should be used when comparing this item with other items. When falsy |
||
114 | * the `label` is used. |
||
115 | */ |
||
116 | public $sortText; |
||
117 | |||
118 | /** |
||
0 ignored issues
–
show
|
|||
119 | * @var array A modifier to the kind which affect how the item is rendered, e.g. Deprecated is rendered |
||
120 | * with a strikeout |
||
121 | */ |
||
122 | public $tags; |
||
123 | |||
124 | // Public Methods |
||
125 | // ========================================================================= |
||
126 | |||
127 | /** |
||
128 | * An optional array of additional text edits that are applied when selecting this completion. |
||
129 | * Edits must not overlap with the main edit nor with themselves. |
||
130 | * ref: https://microsoft.github.io/monaco-editor/api/interfaces/monaco.editor.ISingleEditOperation.html |
||
131 | * @param $value array |
||
0 ignored issues
–
show
|
|||
132 | */ |
||
0 ignored issues
–
show
|
|||
133 | public function additionalTextEdits($value): self |
||
134 | { |
||
135 | $this->additionalTextEdits = $value; |
||
136 | return $this; |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * A command that should be run upon acceptance of this item. |
||
141 | * ref: https://microsoft.github.io/monaco-editor/api/interfaces/monaco.languages.Command.html |
||
142 | * @param $value array |
||
0 ignored issues
–
show
|
|||
143 | */ |
||
0 ignored issues
–
show
|
|||
144 | public function command($value): self |
||
145 | { |
||
146 | $this->command = $value; |
||
147 | return $this; |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * An optional set of characters that when pressed while this completion is active will accept |
||
152 | * it first and then type that character. Note that all commit characters should have `length=1` and that |
||
153 | * superfluous characters will be ignored. |
||
154 | * @param $value array |
||
0 ignored issues
–
show
|
|||
155 | */ |
||
0 ignored issues
–
show
|
|||
156 | public function commitCharacters($value): self |
||
157 | { |
||
158 | $this->commitCharacters = $value; |
||
159 | return $this; |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * A human-readable string with additional information about this item, like type or symbol information. |
||
164 | * @param $value string |
||
0 ignored issues
–
show
|
|||
165 | */ |
||
0 ignored issues
–
show
|
|||
166 | public function detail($value): self |
||
167 | { |
||
168 | $this->detail = $value; |
||
169 | return $this; |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * A human-readable string that represents a doc-comment. |
||
174 | * Can contain Markdown |
||
175 | * @param $value string |
||
0 ignored issues
–
show
|
|||
176 | */ |
||
0 ignored issues
–
show
|
|||
177 | public function documentation($value): self |
||
178 | { |
||
179 | $this->documentation = $value; |
||
180 | return $this; |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * A string that should be used when filtering a set of completion items. |
||
185 | * When falsy the `label` is used. |
||
186 | * @param $value string |
||
0 ignored issues
–
show
|
|||
187 | */ |
||
0 ignored issues
–
show
|
|||
188 | public function filterText($value): self |
||
189 | { |
||
190 | $this->filterText = $value; |
||
191 | return $this; |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * A string or snippet that should be inserted in a document when selecting this completion. |
||
196 | * @param $value string |
||
0 ignored issues
–
show
|
|||
197 | */ |
||
0 ignored issues
–
show
|
|||
198 | public function insertText($value): self |
||
199 | { |
||
200 | $this->insertText = $value; |
||
201 | return $this; |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * Additional rules (as bitmask) that should be applied when inserting this completion. |
||
206 | * @param $value int |
||
0 ignored issues
–
show
|
|||
207 | */ |
||
0 ignored issues
–
show
|
|||
208 | public function insertTextRules($value): self |
||
209 | { |
||
210 | $this->insertTextRules = $value; |
||
211 | return $this; |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * The kind of this completion item. Based on the kind an icon is chosen by the editor. |
||
216 | * @param $value int |
||
0 ignored issues
–
show
|
|||
217 | */ |
||
0 ignored issues
–
show
|
|||
218 | public function kind($value): self |
||
219 | { |
||
220 | $this->kind = $value; |
||
221 | return $this; |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * The label of this completion item. By default this is also the text that is inserted |
||
226 | * when selecting this completion. |
||
227 | * @param $value string |
||
0 ignored issues
–
show
|
|||
228 | */ |
||
0 ignored issues
–
show
|
|||
229 | public function label($value): self |
||
230 | { |
||
231 | $this->label = $value; |
||
232 | return $this; |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * Select this item when showing. Note that only one completion item can be selected and that |
||
237 | * the editor decides which item that is. The rule is that the first item of those that match best is selected. |
||
238 | * @param $value bool |
||
0 ignored issues
–
show
|
|||
239 | */ |
||
0 ignored issues
–
show
|
|||
240 | public function preselect($value): self |
||
241 | { |
||
242 | $this->preselect = $value; |
||
243 | return $this; |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * A range of text that should be replaced by this completion item. |
||
248 | * @param $value array |
||
0 ignored issues
–
show
|
|||
249 | */ |
||
0 ignored issues
–
show
|
|||
250 | public function range($value): self |
||
251 | { |
||
252 | $this->range = $value; |
||
253 | return $this; |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * A string that should be used when comparing this item with other items. When falsy |
||
258 | * the `label` is used. |
||
259 | * @param $value string |
||
0 ignored issues
–
show
|
|||
260 | */ |
||
0 ignored issues
–
show
|
|||
261 | public function sortText($value): self |
||
262 | { |
||
263 | $this->sortText = $value; |
||
264 | return $this; |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * A modifier to the kind which affect how the item is rendered, e.g. Deprecated is rendered |
||
269 | * with a strikeout |
||
270 | * @param $value array |
||
0 ignored issues
–
show
|
|||
271 | */ |
||
0 ignored issues
–
show
|
|||
272 | public function tags($value): self |
||
273 | { |
||
274 | $this->tags = $value; |
||
275 | return $this; |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * Add the completion item to the passed in AutocompleteInterface static class |
||
280 | * |
||
281 | * @param AutocompleteInterface $autocomplete |
||
0 ignored issues
–
show
|
|||
282 | * @param string $path The . delimited path in the autocomplete array to the item; if omitted, will be set to the $item->label |
||
0 ignored issues
–
show
|
|||
283 | * @return void |
||
0 ignored issues
–
show
|
|||
284 | */ |
||
285 | public function add($autocomplete, string $path = ''): void |
||
286 | { |
||
287 | $autocomplete->addCompleteItem($this, $path); |
||
288 | } |
||
289 | |||
290 | /** |
||
0 ignored issues
–
show
|
|||
291 | * @inheritdoc |
||
292 | */ |
||
0 ignored issues
–
show
|
|||
293 | public function defineRules(): array |
||
294 | { |
||
295 | return [ |
||
296 | |||
297 | [ |
||
298 | [ |
||
299 | 'detail', |
||
300 | 'documentation', |
||
301 | 'filterText', |
||
302 | 'insertText', |
||
303 | 'label', |
||
304 | 'sortText', |
||
305 | 'tags', |
||
306 | ], |
||
307 | 'string' |
||
308 | ], |
||
309 | [ |
||
310 | [ |
||
311 | 'additionalTextEdits', |
||
312 | 'command', |
||
313 | 'commitCharacters', |
||
314 | 'range', |
||
315 | ], |
||
316 | ArrayValidator::class |
||
317 | ], |
||
318 | ['insertTextRules', 'integer', 'min' => 0, 'max' => 4], |
||
319 | ['kind', 'integer', 'min' => 0, 'max' => 27], |
||
320 | ['preselect', 'boolean'], |
||
321 | [ |
||
322 | [ |
||
323 | 'insertText', |
||
324 | 'kind', |
||
325 | ], |
||
326 | 'required' |
||
327 | ], |
||
328 | ]; |
||
329 | } |
||
330 | } |
||
331 |