文ごとにハイライトを行うプログラムを書いているのですが、
どうしても、最初の文だけがハイライトで表示され、しかも間違った部分をハイライトしてしまいます。
原因が分からないので、少しでも教えて頂けると幸いです。
※JSONファイルの「=>」の右側を取得し、ハイライトするプログラムを下記に記載しています。
dependencyRelationListとCountは今回は使っていませんので、空が入ります。
<実行結果>
※結果としては、「初期 と (初期 の二つをハイライトしたい。
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 9 const readsemiJson = JSON.parse(fs.readFileSync('C:\Users\specification\src\sample.json','utf8')); 10 11 context.subscriptions.push(vscode.commands.registerCommand('extension.color', () => { 12 vscode.window.showInformationMessage("Color Range Word"); 13 // 特定の単語に色付け 14 const timeWordList = readsemiJson.時間; 15 16 17 const timeWords: vscode.DecorationOptions[] = []; 18 const dependencyRelationWords: vscode.DecorationOptions[] = []; 19 20 var timeCount = 0; 21 22 //child -> parent(値域) semiについて 23 function fromObjecttoRange(WordList: any,foundWordList: vscode.DecorationOptions[],dependencyRelationList: vscode.DecorationOptions[],Count: number){ 24 const activeEditor = vscode.window.activeTextEditor; 25 const text = activeEditor.document.getText(); //ドキュメント取得 26 27 // // 取得したテキストを改行ごと(文章)に分割する 28 // const sentence = text.match(/[^\r\n]*(\r\n|\r|\n|$)/g); 29 var textArray = text.split(/\r\n|\r|\n/); 30 31 if(WordList === timeWordList){ 32 for(var i = 0; i < readsemiJson['時間'].length; i++){ 33 console.log(readsemiJson['時間'][i]); 34 const word = readsemiJson['時間'][i].split('=>'); 35 // const parentword = word[0]; 36 const rangeword = word[1]; 37 for(var j = 0; j < textArray.length; j++){ 38 console.log(textArray[j]); 39 if(textArray[j].indexOf(rangeword)){ 40 const startPos = activeEditor.document.positionAt(textArray[j].indexOf(rangeword)); 41 const endPos = activeEditor.document.positionAt(textArray[j].indexOf(rangeword) + rangeword.length); 42 // console.log(startPos, endPos); 43 const decoration = { range: new vscode.Range(startPos, endPos) }; 44 foundWordList.push(decoration); 45 } 46 } 47 } 48 return {foundWordList,dependencyRelationList,Count}; 49 } 50 } 51 function decorateWord() { 52 const activeEditor = vscode.window.activeTextEditor; 53 // const text = activeEditor.document.getText(); //ドキュメント取得 54 55 //時間 56 fromObjecttoRange(timeWordList,timeWords,dependencyRelationWords,timeCount); 57 58 activeEditor.setDecorations(timeMarkerDecoration,timeWords); 59 } 60 decorateWord(); 61 })); 62 63 //時間に色付け 64 const timeMarkerDecoration = vscode.window.createTextEditorDecorationType({ 65 'borderWidth': '1px', 66 'borderRadius': '2px', 67 'borderStyle': 'solid', 68 // 'backgroundColor': 'rgba(255, 0, 0, 0.3)', 69 'backgroundColor': 'rgba(128, 128, 0, 0.3)', 70 // 'color':'red' 71 'color':'orange' 72 }); 73}
JSON
1{ 2 "時間": [ 3 "なし=>(初期", 4 "なし=>「初期" 5 ] 6}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/04/11 01:57
2019/04/11 02:05
2019/04/11 03:33
2019/04/15 13:58