-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement jumping to messages that need to be update / support messag…
…es with plural forms / Refactoring (#31)
- Loading branch information
Showing
14 changed files
with
738 additions
and
386 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import * as vscode from "vscode"; | ||
import { Message } from "./message_type"; | ||
import { moveCursorTo } from "./moving"; | ||
|
||
export function focusOnNextTarget( | ||
editor: vscode.TextEditor, | ||
nextTargetFunc: ( | ||
editor: vscode.TextDocument, | ||
lineno: number, | ||
backwards: boolean | ||
) => Message, | ||
backwards = false | ||
) { | ||
const position = editor.selection.active; | ||
const message = nextTargetFunc(editor.document, position.line, backwards); | ||
if (message !== null) { | ||
focusOnMessage(editor, message); | ||
} | ||
} | ||
|
||
function focusOnMessage(editor: vscode.TextEditor, message: Message) { | ||
const [line, character] = msgstrPosition(message); | ||
const position = moveCursorTo(editor, line, character); | ||
editor.revealRange( | ||
new vscode.Range(position, position), | ||
vscode.TextEditorRevealType.InCenterIfOutsideViewport | ||
); | ||
} | ||
|
||
function msgstrPosition(message: Message): [number, number] { | ||
for (const [index, line] of message.msgstrPluralLine.entries()) { | ||
if (!message.msgstrPlural[index] || message.isfuzzy) { | ||
return index === 0 | ||
? [line, 11] | ||
: [line, 11 + Math.floor(Math.log10(index))]; | ||
} | ||
} | ||
|
||
return [message.msgstrLine, 8]; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import * as vscode from "vscode"; | ||
import { | ||
nextFuzzyMessage, | ||
nextUntranslatedMessage, | ||
nextUntranslatedOrFuzzyMessage, | ||
} from "./message"; | ||
import { focusOnNextTarget } from "./focusing"; | ||
|
||
export function moveToNextUntranslatedMessage(editor: vscode.TextEditor) { | ||
focusOnNextTarget(editor, nextUntranslatedMessage); | ||
} | ||
|
||
export function moveToPreviousUntranslatedMessage(editor: vscode.TextEditor) { | ||
focusOnNextTarget(editor, nextUntranslatedMessage, true); | ||
} | ||
|
||
export function moveToNextFuzzyMessage(editor: vscode.TextEditor) { | ||
focusOnNextTarget(editor, nextFuzzyMessage); | ||
} | ||
|
||
export function moveToPreviousFuzzyMessage(editor: vscode.TextEditor) { | ||
focusOnNextTarget(editor, nextFuzzyMessage, true); | ||
} | ||
|
||
export function moveToNextUntranslatedOrFuzzyMessage( | ||
editor: vscode.TextEditor | ||
) { | ||
focusOnNextTarget(editor, nextUntranslatedOrFuzzyMessage); | ||
} | ||
|
||
export function moveToPreviousUntranslatedOrFuzzyMessage( | ||
editor: vscode.TextEditor | ||
) { | ||
focusOnNextTarget(editor, nextUntranslatedOrFuzzyMessage, true); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import * as vscode from "vscode"; | ||
|
||
export function* documentLines(document: vscode.TextDocument, startline = 0) { | ||
for (let lineno = startline; lineno < document.lineCount; lineno++) { | ||
yield document.lineAt(lineno); | ||
} | ||
} | ||
|
||
export function* backwardDocumentLines( | ||
document: vscode.TextDocument, | ||
startline = document.lineCount - 1 | ||
) { | ||
for (let lineno = startline; lineno >= 0; lineno--) { | ||
yield document.lineAt(lineno); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import * as vscode from "vscode"; | ||
import { MessageParser } from "./message_parser"; | ||
import { Message } from "./message_type"; | ||
import { documentLines, backwardDocumentLines } from "./lines"; | ||
|
||
export function nextUntranslatedMessage( | ||
document: vscode.TextDocument, | ||
lineno: number, | ||
backwards = false | ||
): Message { | ||
return nextMessageWithCondition(document, lineno, isUntranslated, backwards); | ||
} | ||
|
||
export function nextFuzzyMessage( | ||
document: vscode.TextDocument, | ||
lineno: number, | ||
backwards = false | ||
): Message { | ||
return nextMessageWithCondition( | ||
document, | ||
lineno, | ||
(message: Message) => message.isfuzzy, | ||
backwards | ||
); | ||
} | ||
|
||
export function nextUntranslatedOrFuzzyMessage( | ||
document: vscode.TextDocument, | ||
lineno: number, | ||
backwards = false | ||
): Message { | ||
return nextMessageWithCondition( | ||
document, | ||
lineno, | ||
(message: Message) => isUntranslated(message) || message.isfuzzy, | ||
backwards | ||
); | ||
} | ||
|
||
function nextMessageWithCondition( | ||
document: vscode.TextDocument, | ||
lineno: number, | ||
condition: Function, | ||
backwards = false | ||
): Message { | ||
let message = parseMessage(document, lineno); | ||
const getMessage = backwards ? previousMessage : nextMessage; | ||
while (message !== null) { | ||
message = getMessage(document, message); | ||
if (message && condition(message)) { | ||
return message; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
function nextMessage( | ||
document: vscode.TextDocument, | ||
currentMessage: Message | ||
): Message { | ||
for (const line of documentLines(document, currentMessage.lastline + 1)) { | ||
if (line.text && !line.text.trim().startsWith("#")) { | ||
return parseMessage(document, line.lineNumber); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
function previousMessage( | ||
document: vscode.TextDocument, | ||
currentMessage: Message | ||
): Message { | ||
for (const line of backwardDocumentLines( | ||
document, | ||
currentMessage.firstline - 1 | ||
)) { | ||
if (line.text && !line.text.trim().startsWith("#")) { | ||
return parseMessage(document, line.lineNumber); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
function parseMessage(document: vscode.TextDocument, lineno: number): Message { | ||
return new MessageParser(document, lineno).parse(); | ||
} | ||
|
||
function isUntranslated(message: Message): boolean { | ||
return !isTranslated(message); | ||
} | ||
|
||
function isTranslated(message: Message): boolean { | ||
return ( | ||
(message.msgstr && message.msgstr !== "") || | ||
(message.msgstrPlural && | ||
message.msgstrPlural.length > 0 && | ||
message.msgstrPlural.every((msgstr) => msgstr !== "")) | ||
); | ||
} |
Oops, something went wrong.