Skip to content

Commit

Permalink
Merge pull request #2 from hannut91/fix-letter-cut-bug
Browse files Browse the repository at this point in the history
Fix letter cut bug
  • Loading branch information
hannut91 committed Oct 30, 2019
2 parents d607a56 + 2885e66 commit 11fd863
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 8 deletions.
13 changes: 12 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "korean-spell-checker-vs-code",
"displayName": "Korean Spell Checker VS Code",
"description": "",
"version": "0.2.1",
"version": "0.2.2",
"engines": {
"vscode": "^1.38.0"
},
Expand Down Expand Up @@ -36,13 +36,15 @@
"lint": "./node_modules/.bin/tslint -p ."
},
"devDependencies": {
"@types/lodash": "^4.14.144",
"@types/node": "^12.11.1",
"@types/vscode": "^1.38.0",
"tslint": "^5.20.0",
"typescript": "^3.6.4"
},
"dependencies": {
"axios": "^0.19.0"
"axios": "^0.19.0",
"lodash": "^4.17.15"
},
"publisher": "Yunseok"
}
57 changes: 52 additions & 5 deletions src/commands/spell-fix.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,74 @@
import { window, TextEditorEdit } from 'vscode';
import {
window, TextEditorEdit, Position, Range, Selection, TextLine
} from 'vscode';
import { range, curry } from 'lodash';
import * as _ from 'lodash';

import { SpellCheck } from '../services/spell-checker';

const MAX_TEXT_COUNT = 500;

const getText = curry((textline: TextLine, selection: Selection) => {
const { lineNumber } = textline;
if (lineNumber === selection.start.line) {
return new Range(
selection.start, textline.rangeIncludingLineBreak.end)
}

if (lineNumber === selection.end.line) {
return new Range(textline.range.start, selection.end);
}

return textline.rangeIncludingLineBreak;
})

export const spellFix = async () => {
const editor = window.activeTextEditor;
if (!editor) {
window.showInformationMessage('선택된 에디터가 없습니다.');
return;
}

const text = editor.document.getText(editor.selection);
if (!text) {
if (!editor.document.getText(editor.selection)) {
window.showInformationMessage('선택된 텍스트가 없습니다.');
return;
}

let { selection, document } = editor;

let originText = '';

if (selection.isSingleLine) {
originText = document.getText(selection);
} else {
range(selection.start.line, selection.end.line + 1)
.map(document.lineAt)
.map(getText(_, selection))
.map(document.getText)
.every((text: string, index: number) => {
let result = true;
if (originText.length + text.length >= MAX_TEXT_COUNT) {
text = text.slice(0, MAX_TEXT_COUNT - originText.length);
selection = new Selection(
selection.start,
new Position(selection.start.line + index, text.length),
);
result = false;
}

originText += text;
return result;
});
}

try {
const { notag_html } = await SpellCheck(text);
const { notag_html } = await SpellCheck(originText);

const fixedText = notag_html.replace(/<br>/g, '\n');

editor.edit((editBuilder: TextEditorEdit) => {
editBuilder.replace(editor.selection, fixedText);
editBuilder.replace(selection, fixedText);
editor.selection = selection;
});
} catch (err) {
window.showInformationMessage(
Expand Down

0 comments on commit 11fd863

Please sign in to comment.