vscodeの拡張機能として、
値域を表現する語の係り受け先の語をハイライトするというプログラムを作成しています。
プログラムとしては、あらかじめ用意したjsonファイルを読み込みその情報を元に
特定の語をハイライトするようにしている。
<parent_dict.json>
強時間:[現在=>設置している,
(現在=>設置している
]
というように左辺に値域を表現する語、右辺に係り受け先の語を保存している
困っていることとして
例えば、
・ある場所に現在設置しているものを使用する。
・(現在設置しているものの仕様)
のような2つの文章があった時に、本当であれば
1行目の「現在」と「設置している」、2行目の「(現在」と「設置している」を
ハイライトで表示して欲しいのですが、なぜか、
1行目の「現在」と2行目の「設置している」がハイライトで表示されないというバグが発生しています。
indexOfで探しているからなのか、それとも取得した情報が何かしらで更新されてしまっているのか
原因が分からなくて困っています。
初心者なので、助言を頂けると幸いです。よろしくお願いします。
TypeScript
1'use strict'; 2import * as vscode from 'vscode'; 3import * as fs from 'fs'; 4 5// var decChars: DecChars; 6 7export function activate(context: vscode.ExtensionContext) { 8 console.log('Congratulations, your extension "semieditor" is now active!'); 9 const readJson = JSON.parse(fs.readFileSync('C:\Users\specification\src\parent_dict.json','utf8')); 10 11 context.subscriptions.push(vscode.commands.registerCommand('extension.color', () => { 12 vscode.window.showInformationMessage("Color Range Word"); 13 // 特定の単語に色付け 14 const timeWordList = readJson.時間; 15 16 const timeWords: vscode.DecorationOptions[] = []; 17 const dependencyRelationWords: vscode.DecorationOptions[] = []; 18 19 var timeCount = 0; 20 21 function decorateWord() { 22 const activeEditor = vscode.window.activeTextEditor; 23 const text = activeEditor.document.getText(); //ドキュメント取得 24 25 //強時間 26 for (let i = 0; i < strengthTimeWordList.length; i++){ 27 let word = strengthTimeWordList[i].split('=>'); 28 if(text.indexOf(word[0]) !== -1 && text.indexOf(word[1],text.indexOf(word[0])) !== -1){ 29 30 //値域、状態値を表現する語を抽出 31 const startPos = activeEditor.document.positionAt(text.indexOf(word[0])); 32 const endPos = activeEditor.document.positionAt(text.indexOf(word[0]) + word[0].length); 33 const strengthtime_decoration = { range: new vscode.Range(startPos, endPos)}; 34 strengthTimeWords.push(strengthtime_decoration); 35 36 // 値域、状態値に係る語を抽出 37 const relation_startPos = activeEditor.document.positionAt(text.indexOf(word[1])); 38 const relation_endPos = activeEditor.document.positionAt(text.indexOf(word[1]) + word[1].length); 39 const strengthtime_related_decoration = {range:new vscode.Range(relation_startPos,relation_endPos)}; 40 dependencyRelationWords.push(strengthtime_related_decoration); 41 42 strengthtimeCount++; 43 } 44 } 45 46 47 activeEditor.setDecorations(timeMarkerDecoration,timeWords); 48 activeEditor.setDecorations(dependencyRelation,dependencyRelationWords); 49 50 console.log("強時間:" + strengthtimeCount); 51 } 52 decorateWord(); 53 })); 54 55 //強時間に色付け 56 const strengthTimeMarkerDecoration = vscode.window.createTextEditorDecorationType({ 57 'borderWidth': '1px', 58 'borderRadius': '2px', 59 'borderStyle': 'solid', 60 // 'backgroundColor': 'rgba(0, 0, 255, 0.3)', 61 'backgroundColor': 'rgba(128, 128, 0, 0.3)', 62 // 'color':'blue' 63 'color':'orange' 64 }); 65 66 //係り受けの対象を色付け 67 const dependencyRelation = vscode.window.createTextEditorDecorationType({ 68 'borderWidth': '1px', 69 'borderRadius': '2px', 70 'borderStyle': 'solid', 71 'backgroundColor': 'rgba(0, 255, 0, 0.3)', 72 'color':'lightgreen' 73 }); 74} 75
あなたの回答
tips
プレビュー