Last active
March 26, 2020 16:44
-
-
Save piyo7/6932e8fe52cc276979e2aa159ca96158 to your computer and use it in GitHub Desktop.
Visual Studio Codeの拡張機能を自作して、理想の執筆環境を手に入れた 〆(゚_゚*) ref: http://qiita.com/piyo7/items/4202189d523572da736a
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const decorationIconsColor = vscode.window.createTextEditorDecorationType({ | |
'color': '#c0c0c0' | |
}) | |
let decorationIconsDict: { [key: string]: TextEditorDecorationType } = {} | |
function decorateIcons(editor: TextEditor) { | |
let match: RegExpExecArray = null | |
let iconLineDict: { [key: string]: number[] } = {} | |
let iconLines: number[] = [] | |
const iconRegex = /^(\d\d)[「『]/gm | |
while (match = iconRegex.exec(editor.document.getText())) { | |
const icon = match[1] | |
if (iconLineDict[icon] == null) iconLineDict[icon] = [] | |
iconLineDict[icon].push(editor.document.positionAt(match.index).line) | |
iconLines.push(editor.document.positionAt(match.index).line) | |
} | |
editor.setDecorations( | |
decorationIconsColor, | |
iconLines.map(line => new vscode.Range(line, 0, line, 2))) | |
for (const key in decorationIconsDict) { | |
editor.setDecorations(decorationIconsDict[key], []) | |
} | |
if (iconFlag) { | |
for (const key in iconLineDict) { | |
if (decorationIconsDict[key] == null) { | |
decorationIconsDict[key] = vscode.window.createTextEditorDecorationType({ | |
'gutterIconPath': iconPath(key), | |
'gutterIconSize': 'contain' | |
}) | |
} | |
editor.setDecorations( | |
decorationIconsDict[key], | |
iconLineDict[key].map(line => new vscode.Range(line, 0, line, 0))) | |
} | |
} | |
} | |
function iconPath(name: string): string { | |
return path.join(vscode.workspace.rootPath, '.vscode', 'icon', name + '.png') | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const decorationInvalidTitleColor = vscode.window.createTextEditorDecorationType({ | |
'backgroundColor': '#FF8080' | |
}) | |
function decorateInvalidTitle(editor: TextEditor) { | |
let titleSpeech: [string, number] = null | |
let lineNumbers: number[] = [] | |
for (let i = 0; i < editor.document.lineCount; i++) { | |
const line = editor.document.lineAt(i) | |
if (line.text.length > 0) { | |
if (line.text[0] == '#') { | |
if (titleSpeech != null) { | |
lineNumbers.push(titleSpeech[1]) | |
} | |
const match = /[「『](.+)[」』]/.exec(line.text) | |
if (match != null) { | |
titleSpeech = [match[1], line.lineNumber] | |
} else { | |
titleSpeech = null | |
} | |
} else { | |
if (titleSpeech != null && line.text.includes(titleSpeech[0])) { | |
titleSpeech = null | |
} | |
} | |
} | |
} | |
if (titleSpeech != null) { | |
lineNumbers.push(titleSpeech[1]) | |
} | |
editor.setDecorations( | |
decorationInvalidTitleColor, | |
lineNumbers.map(line => new vscode.Range(line, 0, line + 1, 0))) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"譚丁": "たんてい", | |
"風蛇": "かざへび", | |
"奉双譜": "ほうそうふ", | |
"名塚": "なつか", | |
"令": "れい", | |
"有栖川": "ありすがわ", | |
"和紀": "かずき", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function insertRuby() { | |
const rubyDict: { [key: string]: string } = JSON.parse( | |
fs.readFileSync(path.join(vscode.workspace.rootPath, '.vscode', 'ruby.json'), 'utf8')) | |
const editor = vscode.window.activeTextEditor | |
editor.edit((edit) => { | |
let text = editor.document.getText().replace(/|(.+?)《.+?》/g, '$1') | |
let sections: string[] = [] | |
for (let section of text.split('#')) { | |
for (const key in rubyDict) { | |
section = section.replace(new RegExp(key), '|' + key + '《' + rubyDict[key] + '》') | |
} | |
sections.push(section) | |
} | |
edit.replace(new Range(0, 0, editor.document.lineCount + 1, 0), sections.join("#")) | |
}) | |
} | |
const decorationRubyColor = vscode.window.createTextEditorDecorationType({ | |
'color': '#808080' | |
}) | |
function decorateRuby(editor: TextEditor) { | |
let ranges: Range[] = [] | |
let match: RegExpExecArray | |
const rubyRegex = /|(.+?)(《.+?》)/g | |
while (match = rubyRegex.exec(editor.document.getText())) { | |
const pos = editor.document.positionAt(match.index) | |
ranges.push( | |
new Range(pos.line, pos.character, pos.line, pos.character), | |
new Range( | |
pos.line, pos.character + 1 + match[1].length, | |
pos.line, pos.character + 1 + match[1].length + match[2].length)) | |
} | |
editor.setDecorations(decorationRubyColor, ranges) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function publish() { | |
const editor = vscode.window.activeTextEditor | |
editor.edit((edit) => { | |
edit.replace(new Range(0, 0, editor.document.lineCount + 1, 0), editor.document.getText(). | |
replace(/^\d\d([「『])/gm, '$1'). | |
replace(/([」』])$/gm, '$1\r\n'). | |
replace(/\r\n^---$/gm, ' ~~~')) | |
}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function displayWordCount() { | |
const document = vscode.window.activeTextEditor.document | |
let count = 0 | |
for (let i = 0; i < document.lineCount; i++) { | |
const line = document.lineAt(i).text | |
if (line.length > 0 && line[0] != '#') { | |
for (let j = 0; j < line.length; j++) { | |
if (line.charCodeAt(j) > 127) count++ | |
} | |
} | |
} | |
vscode.window.setStatusBarMessage(count.toString() + "文字") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment