私はjsonファイルから読み込んだ特定の語をハイライトで表示するという
プログラムを書いています。
取得したテキストを改行で分割し、文章ごとにハイライトしたい単語があるかどうか判定させよう
としているが、その判定が上手くいかず、ハイライトがうまくいきません。
原因が分からないので教えて頂けると幸いです。
※jsonファイルの「⇒」の左側の単語がオレンジ色、右側が緑色にハイライトするように
プログラムの処理をしている。
typescript
1'use strict'; 2import * as vscode from 'vscode'; 3import * as fs from 'fs'; 4 5 6export function activate(context: vscode.ExtensionContext) { 7 console.log('Congratulations, your extension "semieditor" is now active!'); 8 const readsemiJson = JSON.parse(fs.readFileSync('C:\Users\specification\src\sample.json','utf8')); 9 10 context.subscriptions.push(vscode.commands.registerCommand('extension.color', () => { 11 vscode.window.showInformationMessage("Color Range Word"); 12 // 特定の単語に色付け 13 const timeWordList = readsemiJson.時間; 14 const strengthTimeWordList = readsemiJson.強時間; 15 const futurePhraseWordList = readsemiJson.未来句; 16 const quantityWordList = readsemiJson.数量; 17 const numberOfHoursWordList = readsemiJson.時数ノ; 18 const strengthQuantityQualifiedWordList = readsemiJson.強数量修飾; 19 const numberOfTimeWordList = readsemiJson.回数; 20 21 var timeCount = 0; 22 var strengthtimeCount = 0; 23 var futurephraseCount = 0; 24 var quantityCount = 0; 25 var numberofhoursCount = 0; 26 var strengthquantityqualifiedCount = 0; 27 var numberoftimeCount = 0; 28 29 //child -> parent(値域) semiについて 30 function fromObjecttoRange(WordList: any,foundWordList: vscode.DecorationOptions[],dependencyRelationList: vscode.DecorationOptions[],Count: number){ 31 const activeEditor = vscode.window.activeTextEditor; 32 const text = activeEditor.document.getText(); //ドキュメント取得 33 34 // 取得したテキストを改行ごと(文章)に分割する 35 const sentence = text.match(/[^\r\n]*(\r\n|\r|\n|$)/g); 36 37 for(let i = 0; i < sentence.length; i++){ 38 console.log(sentence[i]); 39 for (let j = 0; j < WordList.length; j++){ 40 let word = WordList[j].split('=>'); 41 if(sentence[i].indexOf(word[0]) !== -1 && sentence[i].indexOf(word[1]) !== -1){ 42 43 //値域、状態値を表現する語を抽出 44 const startPos = activeEditor.document.positionAt(sentence[i].indexOf(word[1])); 45 const endPos = activeEditor.document.positionAt(sentence[i].indexOf(word[1]) + word[1].length); 46 const decoration = { range: new vscode.Range(startPos, endPos)}; 47 activeEditor.setDecorations(MarkerDecoration,[decoration]); 48 49 // 値域、状態値に係る語を抽出 50 const relation_startPos = activeEditor.document.positionAt(sentence[i].indexOf(word[0])); 51 const relation_endPos = activeEditor.document.positionAt(sentence[i].indexOf(word[0]) + word[0].length); 52 const related_decoration = {range:new vscode.Range(relation_startPos,relation_endPos)}; 53 activeEditor.setDecorations(dependencyRelation,[related_decoration]); 54 } 55 } 56 } 57 return {foundWordList,dependencyRelationList,Count}; 58 59 } 60 61 function decorateWord() { 62 // const activeEditor = vscode.window.activeTextEditor; 63 // const text = activeEditor.document.getText(); //ドキュメント取得 64 65 //時間 66 fromObjecttoRange(timeWordList,timeWords,dependencyRelationWords,timeCount); 67 //強時間 68 fromObjecttoRange(strengthTimeWordList,strengthTimeWords,dependencyRelationWords,strengthtimeCount); 69 //未来句 70 fromObjecttoRange(futurePhraseWordList,futurePhraseWords,dependencyRelationWords,futurephraseCount); 71 // 数量 72 fromObjecttoRange(quantityWordList,quantityWords,dependencyRelationWords,quantityCount); 73 //時数ノ 74 fromObjecttoRange(numberOfHoursWordList,numberOfHoursWords,dependencyRelationWords,numberofhoursCount); 75 //強数量修飾 76 fromObjecttoRange(strengthQuantityQualifiedWordList,strengthQuantityQualifiedWords,dependencyRelationWords,strengthquantityqualifiedCount); 77 //回数 78 fromObjecttoRange(numberOfTimeWordList,numberOfTimeWords,dependencyRelationWords,numberoftimeCount); 79 } 80 decorateWord(); 81 })); 82 83 const MarkerDecoration = vscode.window.createTextEditorDecorationType({ 84 'borderWidth': '1px', 85 'borderRadius': '2px', 86 'borderStyle': 'solid', 87 // 'backgroundColor': 'rgba(255, 0, 0, 0.3)', 88 'backgroundColor': 'rgba(128, 128, 0, 0.3)', 89 // 'color':'red' 90 'color':'orange' 91 }); 92 93 //係り受けの対象を色付け 94 const dependencyRelation = vscode.window.createTextEditorDecorationType({ 95 'borderWidth': '1px', 96 'borderRadius': '2px', 97 'borderStyle': 'solid', 98 'backgroundColor': 'rgba(0, 255, 0, 0.3)', 99 'color':'lightgreen' 100 }); 101}
JSON
1{ 2 "回数": [ 3 ], 4 "強数量修飾": [], 5 "時数ノ": [ 6 ], 7 "未来句": [], 8 "数量": [ 9 ], 10 "強時間": [ 11 "ドア入力右=>常時", 12 "なし=>長時間", 13 "ドア入力I=>常時" 14 ], 15 "時間": [ 16 ] 17} 18
あなたの回答
tips
プレビュー