質問編集履歴

2

プログラムの説明の追加

2019/05/09 09:14

投稿

syen2501
syen2501

スコア38

test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,12 @@
1
+ 私は、特定の語を文ごとにハイライトするというプログラムを作成しています。
2
+
3
+ それで、ハイライトした結果から、ユーザーが単語を指定してハイライトのON・OFFが
4
+
5
+ 出来るように拡張をしようとしています。
6
+
7
+
8
+
1
- function decorateWord(boolean: boolean) の部分で「true」と「false」によって
9
+ 方法としては、function decorateWord(boolean: boolean) の部分で「true」と「false」によって
2
10
 
3
11
  ハイライトのON・OFFをしようとしているのですが上手くいかずに困っています。
4
12
 

1

typescriptのプログラムと困っている部分の説明について変更

2019/05/09 09:14

投稿

syen2501
syen2501

スコア38

test CHANGED
File without changes
test CHANGED
@@ -1,18 +1,6 @@
1
- 私は、特定の単語を文ごとにハイライトするというプログラムを作成しました。
2
-
3
- プログラム、ハイライトで表示しいる単語の中から、ユーザが指定して
1
+ function decorateWord(boolean: boolean) 部分で「true」と「false」よっ
4
-
2
+
5
- ハイライトを消す機能を作成しようと考えています。
3
+ ハイライトのON・OFFをしようとているのですが上手くいかずに困っています。
6
-
7
-
8
-
9
- 1つの案として、下記のプログラムの activeEditor.setDecorations の部分で
10
-
11
- ハイライトを行うと思っているのですが、ハイライトの色を決める引数に空文字 or nullを
12
-
13
- 入れればハイライトが消えると思っています。
14
-
15
-
16
4
 
17
5
  初心者なので、出来れば方法等を教えて頂ければ幸いですので、宜しくお願い致します。
18
6
 
@@ -34,9 +22,203 @@
34
22
 
35
23
  console.log('Congratulations, your extension "semieditor" is now active!');
36
24
 
37
-
38
-
39
- const readsemiJson = JSON.parse(fs.readFileSync('C:\Users\specification\src\sample.json','utf8'));
25
+ const readsemiJson = JSON.parse(fs.readFileSync('C:\Users\神龍\specification\src\A_REF_扉開閉機能.json','utf8'));
26
+
27
+
28
+
29
+ // 特定の単語に色付け
30
+
31
+ const timeWordList = readsemiJson.時間;
32
+
33
+ const strengthTimeWordList = readsemiJson.強時間;
34
+
35
+ const futurePhraseWordList = readsemiJson.未来句;
36
+
37
+ const quantityWordList = readsemiJson.数量;
38
+
39
+ const numberOfHoursWordList = readsemiJson.時数ノ;
40
+
41
+ const strengthQuantityQualifiedWordList = readsemiJson.強数量修飾;
42
+
43
+ const numberOfTimeWordList = readsemiJson.回数;
44
+
45
+
46
+
47
+ const timeWords: vscode.DecorationOptions[] = [];
48
+
49
+ const strengthTimeWords: vscode.DecorationOptions[] = [];
50
+
51
+ const futurePhraseWords: vscode.DecorationOptions[] = [];
52
+
53
+ const quantityWords: vscode.DecorationOptions[] = [];
54
+
55
+ const numberOfHoursWords: vscode.DecorationOptions[] = [];
56
+
57
+ const strengthQuantityQualifiedWords: vscode.DecorationOptions[] = [];
58
+
59
+ const numberOfTimeWords: vscode.DecorationOptions[] = [];
60
+
61
+ const dependencyRelationWords: vscode.DecorationOptions[] = [];
62
+
63
+
64
+
65
+ var timeCount = 0;
66
+
67
+ var strengthtimeCount = 0;
68
+
69
+ var futurephraseCount = 0;
70
+
71
+ var quantityCount = 0;
72
+
73
+ var numberofhoursCount = 0;
74
+
75
+ var strengthquantityqualifiedCount = 0;
76
+
77
+ var numberoftimeCount = 0;
78
+
79
+
80
+
81
+ //child -> parent(値域) semiについて
82
+
83
+ function fromObjecttoRange(WordList: any,foundWordList: vscode.DecorationOptions[],dependencyRelationList: vscode.DecorationOptions[],Count: number){
84
+
85
+ const activeEditor = vscode.window.activeTextEditor;
86
+
87
+ const text = activeEditor.document.getText(); //ドキュメント取得
88
+
89
+
90
+
91
+ // 取得したテキストを改行ごと(文章)に分割する
92
+
93
+ var textArray = text.split(/\r\n|\r|\n/);
94
+
95
+
96
+
97
+ if(WordList.length > 0){
98
+
99
+ for(var i = 0; i < WordList.length; i++){
100
+
101
+ const word = WordList[i].split('=>');
102
+
103
+ const parentword = word[0];
104
+
105
+ const rangeword = word[1];
106
+
107
+ var offSet = 0;
108
+
109
+ for(var j = 0; j < textArray.length; j++){
110
+
111
+ if(textArray[j] !== ''){
112
+
113
+ if(textArray[j].search(rangeword) !== -1 && textArray[j].search(parentword) !== -1){
114
+
115
+ const startPos = activeEditor.document.positionAt(offSet + textArray[j].search(rangeword));
116
+
117
+ const endPos = activeEditor.document.positionAt(offSet + textArray[j].search(rangeword) + rangeword.length);
118
+
119
+ const decoration = { range: new vscode.Range(startPos, endPos) };
120
+
121
+ foundWordList.push(decoration);
122
+
123
+
124
+
125
+ const related_startPos = activeEditor.document.positionAt(offSet + textArray[j].search(parentword));
126
+
127
+ const related_endPos = activeEditor.document.positionAt(offSet + textArray[j].search(parentword) + parentword.length);
128
+
129
+ const related_decoration = { range: new vscode.Range(related_startPos, related_endPos) };
130
+
131
+ dependencyRelationList.push(related_decoration);
132
+
133
+ }
134
+
135
+ if(textArray[j].search(rangeword) !== -1 && parentword === 'なし'){
136
+
137
+ const startPos = activeEditor.document.positionAt(offSet + textArray[j].search(rangeword));
138
+
139
+ const endPos = activeEditor.document.positionAt(offSet + textArray[j].search(rangeword) + rangeword.length);
140
+
141
+ const decoration = { range: new vscode.Range(startPos, endPos) };
142
+
143
+ foundWordList.push(decoration);
144
+
145
+ }
146
+
147
+ }
148
+
149
+ //改行の長さも加算している
150
+
151
+ offSet += textArray[j].length + 2;
152
+
153
+ Count++;
154
+
155
+ }
156
+
157
+ }
158
+
159
+ return {foundWordList,dependencyRelationList,Count};
160
+
161
+ }
162
+
163
+ }
164
+
165
+
166
+
167
+ //値域と修飾語をハイライト
168
+
169
+ function decorateWord(boolean: boolean) {
170
+
171
+ const activeEditor = vscode.window.activeTextEditor;
172
+
173
+
174
+
175
+ //時間
176
+
177
+ fromObjecttoRange(timeWordList,timeWords,dependencyRelationWords,timeCount);
178
+
179
+ //強時間
180
+
181
+ fromObjecttoRange(strengthTimeWordList,strengthTimeWords,dependencyRelationWords,strengthtimeCount);
182
+
183
+ //未来句
184
+
185
+ fromObjecttoRange(futurePhraseWordList,futurePhraseWords,dependencyRelationWords,futurephraseCount);
186
+
187
+ //数量
188
+
189
+ fromObjecttoRange(quantityWordList,quantityWords,dependencyRelationWords,quantityCount);
190
+
191
+ //時数ノ
192
+
193
+ fromObjecttoRange(numberOfHoursWordList,numberOfHoursWords,dependencyRelationWords,numberofhoursCount);
194
+
195
+ //強数量修飾
196
+
197
+ fromObjecttoRange(strengthQuantityQualifiedWordList,strengthQuantityQualifiedWords,dependencyRelationWords,strengthquantityqualifiedCount);
198
+
199
+ //回数
200
+
201
+ fromObjecttoRange(numberOfTimeWordList,numberOfTimeWords,dependencyRelationWords,numberoftimeCount);
202
+
203
+
204
+
205
+ activeEditor.setDecorations(timeMarkerDecoration,timeWords);
206
+
207
+ activeEditor.setDecorations(strengthTimeMarkerDecoration,strengthTimeWords);
208
+
209
+ activeEditor.setDecorations(futurePhraseMarkerDecoration,futurePhraseWords);
210
+
211
+ activeEditor.setDecorations(quantityMarkerDecoration,quantityWords);
212
+
213
+ activeEditor.setDecorations(numberOfHoursMarkerDecoration,numberOfHoursWords);
214
+
215
+ activeEditor.setDecorations(strengthQuantityQualifiedMarkerDecoration,strengthQuantityQualifiedWords);
216
+
217
+ activeEditor.setDecorations(numberOfTimesMarkerDecoration,numberOfTimeWords);
218
+
219
+ activeEditor.setDecorations(dependencyRelation,dependencyRelationWords);
220
+
221
+ }
40
222
 
41
223
 
42
224
 
@@ -44,267 +226,187 @@
44
226
 
45
227
  vscode.window.showInformationMessage("Color Range Word");
46
228
 
47
- // 特定の単語に色付け
48
-
49
- const timeWordList = readsemiJson.時間;
50
-
51
- const strengthTimeWordList = readsemiJson.強時間;
52
-
53
- const futurePhraseWordList = readsemiJson.未来句;
54
-
55
- const quantityWordList = readsemiJson.数量;
56
-
57
- const numberOfHoursWordList = readsemiJson.時数ノ;
58
-
59
- const strengthQuantityQualifiedWordList = readsemiJson.強数量修飾;
60
-
61
- const numberOfTimeWordList = readsemiJson.回数;
62
-
63
-
64
-
65
- const timeWords: vscode.DecorationOptions[] = [];
66
-
67
- const strengthTimeWords: vscode.DecorationOptions[] = [];
68
-
69
- const futurePhraseWords: vscode.DecorationOptions[] = [];
70
-
71
- const quantityWords: vscode.DecorationOptions[] = [];
72
-
73
- const numberOfHoursWords: vscode.DecorationOptions[] = [];
74
-
75
- const strengthQuantityQualifiedWords: vscode.DecorationOptions[] = [];
76
-
77
- const numberOfTimeWords: vscode.DecorationOptions[] = [];
78
-
79
- const dependencyRelationWords: vscode.DecorationOptions[] = [];
80
-
81
-
82
-
83
- var timeCount = 0;
84
-
85
- var strengthtimeCount = 0;
86
-
87
- var futurephraseCount = 0;
88
-
89
- var quantityCount = 0;
90
-
91
- var numberofhoursCount = 0;
92
-
93
- var strengthquantityqualifiedCount = 0;
94
-
95
- var numberoftimeCount = 0;
96
-
97
-
98
-
99
- //child -> parent(値域) semiについて
100
-
101
- function fromObjecttoRange(WordList: any,foundWordList: vscode.DecorationOptions[],dependencyRelationList: vscode.DecorationOptions[],Count: number){
102
-
103
- const activeEditor = vscode.window.activeTextEditor;
104
-
105
- const text = activeEditor.document.getText(); //ドキュメント取得
106
-
107
-
108
-
109
- // // 取得したテキストを改行ごと(文章)に分割する
110
-
111
- var textArray = text.split(/\r\n|\r|\n/);
112
-
113
-
114
-
115
- if(WordList.length > 0){
116
-
117
- console.log(WordList);
118
-
119
- for(var i = 0; i < WordList.length; i++){
120
-
121
- const word = WordList[i].split('=>');
122
-
123
- const parentword = word[0];
124
-
125
- const rangeword = word[1];
126
-
127
- var offSet = 0;
128
-
129
- for(var j = 0; j < textArray.length; j++){
130
-
131
- if(textArray[j] !== ''){
132
-
133
- if(textArray[j].search(rangeword) !== -1 && textArray[j].search(parentword) !== -1){
134
-
135
- const startPos = activeEditor.document.positionAt(offSet + textArray[j].search(rangeword));
136
-
137
- const endPos = activeEditor.document.positionAt(offSet + textArray[j].search(rangeword) + rangeword.length);
138
-
139
- const decoration = { range: new vscode.Range(startPos, endPos) };
140
-
141
- foundWordList.push(decoration);
142
-
143
- console.log(rangeword);
144
-
145
- console.log('値域\n');
146
-
147
- console.log(startPos);
148
-
149
- console.log(endPos);
150
-
151
-
152
-
153
- const related_startPos = activeEditor.document.positionAt(offSet + textArray[j].search(parentword));
154
-
155
- const related_endPos = activeEditor.document.positionAt(offSet + textArray[j].search(parentword) + parentword.length);
156
-
157
- const related_decoration = { range: new vscode.Range(related_startPos, related_endPos) };
158
-
159
- dependencyRelationList.push(related_decoration);
160
-
161
- console.log(parentword);
162
-
163
- console.log('修飾語\n');
164
-
165
- console.log(related_startPos);
166
-
167
- console.log(related_endPos);
168
-
169
- }
170
-
171
- if(textArray[j].search(rangeword) !== -1 && parentword === 'なし'){
172
-
173
- const startPos = activeEditor.document.positionAt(offSet + textArray[j].search(rangeword));
174
-
175
- const endPos = activeEditor.document.positionAt(offSet + textArray[j].search(rangeword) + rangeword.length);
176
-
177
- const decoration = { range: new vscode.Range(startPos, endPos) };
178
-
179
- foundWordList.push(decoration);
180
-
181
- console.log(rangeword);
182
-
183
- console.log('値域\n');
184
-
185
- console.log(startPos);
186
-
187
- console.log(endPos);
188
-
189
- }
190
-
191
- }
192
-
193
- //改行の長さも加算している
194
-
195
- offSet += textArray[j].length + 2;
196
-
197
- Count++;
198
-
199
- }
200
-
201
- }
202
-
203
- return {foundWordList,dependencyRelationList,Count};
204
-
205
- }
206
-
207
- }
208
-
209
-
210
-
211
- //値域と修飾語をハイライト
212
-
213
- function decorateWord() {
214
-
215
- const activeEditor = vscode.window.activeTextEditor;
216
-
217
-
218
-
219
- //時間
220
-
221
- fromObjecttoRange(timeWordList,timeWords,dependencyRelationWords,timeCount);
222
-
223
- //強時間
224
-
225
- fromObjecttoRange(strengthTimeWordList,strengthTimeWords,dependencyRelationWords,strengthtimeCount);
226
-
227
- //未来句
228
-
229
- fromObjecttoRange(futurePhraseWordList,futurePhraseWords,dependencyRelationWords,futurephraseCount);
230
-
231
- //数量
232
-
233
- fromObjecttoRange(quantityWordList,quantityWords,dependencyRelationWords,quantityCount);
234
-
235
- //時数ノ
236
-
237
- fromObjecttoRange(numberOfHoursWordList,numberOfHoursWords,dependencyRelationWords,numberofhoursCount);
238
-
239
- //強数量修飾
240
-
241
- fromObjecttoRange(strengthQuantityQualifiedWordList,strengthQuantityQualifiedWords,dependencyRelationWords,strengthquantityqualifiedCount);
242
-
243
- //回数
244
-
245
- fromObjecttoRange(numberOfTimeWordList,numberOfTimeWords,dependencyRelationWords,numberoftimeCount);
246
-
247
-
248
-
249
- activeEditor.setDecorations(rangeMarkerDecoration,timeWords);
250
-
251
- activeEditor.setDecorations(rangeMarkerDecoration,strengthTimeWords);
252
-
253
- activeEditor.setDecorations(rangeMarkerDecoration,futurePhraseWords);
254
-
255
- activeEditor.setDecorations(rangeMarkerDecoration,quantityWords);
256
-
257
- activeEditor.setDecorations(rangeMarkerDecoration,numberOfHoursWords);
258
-
259
- activeEditor.setDecorations(rangeMarkerDecoration,strengthQuantityQualifiedWords);
260
-
261
- activeEditor.setDecorations(rangeMarkerDecoration,numberOfTimeWords);
262
-
263
- activeEditor.setDecorations(dependencyRelation,dependencyRelationWords);
264
-
265
- }
266
-
267
- decorateWord();
229
+ decorateWord(true);
268
230
 
269
231
  }));
270
232
 
271
- //値域に色付け
272
-
273
- const rangeMarkerDecoration = vscode.window.createTextEditorDecorationType({
274
-
275
- 'borderWidth': '1px',
276
-
277
- 'borderRadius': '2px',
278
-
279
- 'borderStyle': 'solid',
280
-
281
- // 'backgroundColor': 'rgba(255, 0, 0, 0.3)',
282
-
283
- 'backgroundColor': 'rgba(128, 128, 0, 0.3)',
284
-
285
- // 'color':'red'
286
-
287
- 'color':'orange'
288
-
289
- });
290
-
291
-
292
-
293
- //係り受けの対象を色付け
294
-
295
- const dependencyRelation = vscode.window.createTextEditorDecorationType({
296
-
297
- 'borderWidth': '1px',
298
-
299
- 'borderRadius': '2px',
300
-
301
- 'borderStyle': 'solid',
302
-
303
- 'backgroundColor': 'rgba(0, 255, 0, 0.3)',
304
-
305
- 'color':'lightgreen'
306
-
307
- });
233
+
234
+
235
+ context.subscriptions.push(vscode.commands.registerCommand('extension.highlightOFF', () => {
236
+
237
+ vscode.window.showInformationMessage("HighLight OFF");
238
+
239
+ decorateWord(false);
240
+
241
+ }));
242
+
243
+
244
+
245
+ //時間に色付け
246
+
247
+ const timeMarkerDecoration = vscode.window.createTextEditorDecorationType({
248
+
249
+ 'borderWidth': '1px',
250
+
251
+ 'borderRadius': '2px',
252
+
253
+ 'borderStyle': 'solid',
254
+
255
+ // 'backgroundColor': 'rgba(255, 0, 0, 0.3)',
256
+
257
+ 'backgroundColor': 'rgba(128, 128, 0, 0.3)',
258
+
259
+ // 'color':'red'
260
+
261
+ 'color':'orange'
262
+
263
+ });
264
+
265
+
266
+
267
+ //強時間に色付け
268
+
269
+ const strengthTimeMarkerDecoration = vscode.window.createTextEditorDecorationType({
270
+
271
+ 'borderWidth': '1px',
272
+
273
+ 'borderRadius': '2px',
274
+
275
+ 'borderStyle': 'solid',
276
+
277
+ // 'backgroundColor': 'rgba(0, 0, 255, 0.3)',
278
+
279
+ 'backgroundColor': 'rgba(128, 128, 0, 0.3)',
280
+
281
+ // 'color':'blue'
282
+
283
+ 'color':'orange'
284
+
285
+ });
286
+
287
+
288
+
289
+ //未来句に色付け
290
+
291
+ const futurePhraseMarkerDecoration = vscode.window.createTextEditorDecorationType({
292
+
293
+ 'borderWidth': '1px',
294
+
295
+ 'borderRadius': '2px',
296
+
297
+ 'borderStyle': 'solid',
298
+
299
+ // 'backgroundColor': 'rgba(0, 255, 0, 0.3)',
300
+
301
+ 'backgroundColor': 'rgba(128, 128, 0, 0.3)',
302
+
303
+ // 'color':'lightgreen'
304
+
305
+ 'color':'orange'
306
+
307
+ });
308
+
309
+
310
+
311
+ //数量に色付け
312
+
313
+ const quantityMarkerDecoration = vscode.window.createTextEditorDecorationType({
314
+
315
+ 'borderWidth': '1px',
316
+
317
+ 'borderRadius': '2px',
318
+
319
+ 'borderStyle': 'solid',
320
+
321
+ // 'backgroundColor': 'rgba(255, 255, 0, 0.3)',
322
+
323
+ 'backgroundColor': 'rgba(128, 128, 0, 0.3)',
324
+
325
+ // 'color':'yellow'
326
+
327
+ 'color':'orange'
328
+
329
+ });
330
+
331
+
332
+
333
+ //時数ノに色付け
334
+
335
+ const numberOfHoursMarkerDecoration = vscode.window.createTextEditorDecorationType({
336
+
337
+ 'borderWidth': '1px',
338
+
339
+ 'borderRadius': '2px',
340
+
341
+ 'borderStyle': 'solid',
342
+
343
+ // 'backgroundColor': 'rgba(0, 255, 255, 0.3)',
344
+
345
+ 'backgroundColor': 'rgba(128, 128, 0, 0.3)',
346
+
347
+ // 'color':'lightblue'
348
+
349
+ 'color':'orange'
350
+
351
+ });
352
+
353
+
354
+
355
+ //強数量修飾に色付け
356
+
357
+ const strengthQuantityQualifiedMarkerDecoration = vscode.window.createTextEditorDecorationType({
358
+
359
+ 'borderWidth': '1px',
360
+
361
+ 'borderRadius': '2px',
362
+
363
+ 'borderStyle': 'solid',
364
+
365
+ // 'backgroundColor': 'rgba(255, 0, 255, 0.3)',
366
+
367
+ 'backgroundColor': 'rgba(128, 128, 0, 0.3)',
368
+
369
+ // 'color':'purple'
370
+
371
+ 'color':'orange'
372
+
373
+ });
374
+
375
+
376
+
377
+ //回数に色付け
378
+
379
+ const numberOfTimesMarkerDecoration = vscode.window.createTextEditorDecorationType({
380
+
381
+ 'borderWidth': '1px',
382
+
383
+ 'borderRadius': '2px',
384
+
385
+ 'borderStyle': 'solid',
386
+
387
+ 'backgroundColor': 'rgba(128, 128, 0, 0.3)',
388
+
389
+ 'color':'orange'
390
+
391
+ });
392
+
393
+
394
+
395
+ //係り受けの対象を色付け
396
+
397
+ const dependencyRelation = vscode.window.createTextEditorDecorationType({
398
+
399
+ 'borderWidth': '1px',
400
+
401
+ 'borderRadius': '2px',
402
+
403
+ 'borderStyle': 'solid',
404
+
405
+ 'backgroundColor': 'rgba(0, 255, 0, 0.3)',
406
+
407
+ 'color':'lightgreen'
408
+
409
+ });
308
410
 
309
411
  }
310
412