Skip to content

Commit

Permalink
feat: click to navigate to detection, close #509
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Mar 31, 2021
1 parent 0905d77 commit 60c0f8a
Show file tree
Hide file tree
Showing 26 changed files with 116 additions and 35 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,5 @@
"vue",
"html"
],
"typescript.tsdk": "node_modules/typescript/lib",
}
1 change: 1 addition & 0 deletions locales/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@
"review.translation_candidates": "Übersetzungskanditaten",
"review.unknown_user": "Unbekannt",
"view.current_file": "Aktuelle Datei",
"view.current_file_hard_strings": "",
"view.current_file_hard_strings_unknown": "",
"view.current_file_keys_in_use": "",
"view.current_file_keys_not_found": "",
Expand Down
2 changes: 2 additions & 0 deletions locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"command.duplicate_key": "Duplicate key",
"command.edit_key": "Edit translation",
"command.fulfill_keys": "Fulfill keys with empty strings",
"command.go_to_range": "Go to range",
"command.insert_key": "Insert key",
"command.locale_visibility_hide": "Hide locale",
"command.locale_visibility_show": "Show locale",
Expand Down Expand Up @@ -199,6 +200,7 @@
"review.unknown_user": "Unknown",
"view.current_file": "Current file",
"view.current_file_hard_strings": "Hard-coded strings",
"view.current_file_hard_strings_unknown": "",
"view.current_file_keys_in_use": "Keys in use",
"view.current_file_keys_not_found": "Keys not found",
"view.help_feedback": "Help & Feedback",
Expand Down
1 change: 1 addition & 0 deletions locales/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@
"review.translation_candidates": "Traducciones candidatas",
"review.unknown_user": "Desconocido",
"view.current_file": "Archivo actual",
"view.current_file_hard_strings": "",
"view.current_file_hard_strings_unknown": "",
"view.current_file_keys_in_use": "",
"view.current_file_keys_not_found": "",
Expand Down
1 change: 1 addition & 0 deletions locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@
"review.translation_candidates": "",
"review.unknown_user": "",
"view.current_file": "Fichier ouvert",
"view.current_file_hard_strings": "",
"view.current_file_hard_strings_unknown": "",
"view.current_file_keys_in_use": "",
"view.current_file_keys_not_found": "",
Expand Down
1 change: 1 addition & 0 deletions locales/ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@
"review.translation_candidates": "",
"review.unknown_user": "",
"view.current_file": "現在のファイル",
"view.current_file_hard_strings": "",
"view.current_file_hard_strings_unknown": "",
"view.current_file_keys_in_use": "",
"view.current_file_keys_not_found": "",
Expand Down
1 change: 1 addition & 0 deletions locales/nb-NO.json
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@
"review.translation_candidates": "",
"review.unknown_user": "",
"view.current_file": "Denne filen",
"view.current_file_hard_strings": "",
"view.current_file_hard_strings_unknown": "",
"view.current_file_keys_in_use": "",
"view.current_file_keys_not_found": "",
Expand Down
1 change: 1 addition & 0 deletions locales/nl-NL.json
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@
"review.translation_candidates": "",
"review.unknown_user": "",
"view.current_file": "Huidige bestand",
"view.current_file_hard_strings": "",
"view.current_file_hard_strings_unknown": "",
"view.current_file_keys_in_use": "",
"view.current_file_keys_not_found": "",
Expand Down
1 change: 1 addition & 0 deletions locales/pt-BR.json
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@
"review.translation_candidates": "",
"review.unknown_user": "",
"view.current_file": "Arquivo atual",
"view.current_file_hard_strings": "",
"view.current_file_hard_strings_unknown": "",
"view.current_file_keys_in_use": "",
"view.current_file_keys_not_found": "",
Expand Down
1 change: 1 addition & 0 deletions locales/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@
"review.translation_candidates": "Кандидаты на перевод",
"review.unknown_user": "Неизвестно",
"view.current_file": "Текущий файл",
"view.current_file_hard_strings": "",
"view.current_file_hard_strings_unknown": "",
"view.current_file_keys_in_use": "",
"view.current_file_keys_not_found": "",
Expand Down
1 change: 1 addition & 0 deletions locales/sv-SE.json
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@
"review.translation_candidates": "Översättningskandidater",
"review.unknown_user": "Okänd",
"view.current_file": "Nuvarande fil",
"view.current_file_hard_strings": "",
"view.current_file_hard_strings_unknown": "",
"view.current_file_keys_in_use": "",
"view.current_file_keys_not_found": "",
Expand Down
1 change: 1 addition & 0 deletions locales/uk-UA.json
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@
"review.translation_candidates": "Кандидати на переклад",
"review.unknown_user": "Невідомо",
"view.current_file": "Поточний файл",
"view.current_file_hard_strings": "",
"view.current_file_hard_strings_unknown": "",
"view.current_file_keys_in_use": "",
"view.current_file_keys_not_found": "",
Expand Down
1 change: 1 addition & 0 deletions locales/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@
"review.translation_candidates": "候选翻译",
"review.unknown_user": "未知",
"view.current_file": "当前文件",
"view.current_file_hard_strings": "",
"view.current_file_hard_strings_unknown": "",
"view.current_file_keys_in_use": "",
"view.current_file_keys_not_found": "",
Expand Down
1 change: 1 addition & 0 deletions locales/zh-TW.json
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@
"review.translation_candidates": "翻譯候選",
"review.unknown_user": "未知",
"view.current_file": "當前檔案",
"view.current_file_hard_strings": "",
"view.current_file_hard_strings_unknown": "",
"view.current_file_keys_in_use": "",
"view.current_file_keys_not_found": "",
Expand Down
9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,11 @@
"command": "i18n-ally.deepl-usage",
"category": "%extname%",
"title": "%command.deepl_usage%"
},
{
"command": "i18n-ally.go-to-range",
"category": "%extname%",
"title": "%command.go_to_range%"
}
],
"menus": {
Expand Down Expand Up @@ -446,6 +451,10 @@
{
"command": "i18n-ally.duplicate-key",
"when": "false"
},
{
"command": "i18n-ally.go-to-range",
"when": "false"
}
],
"view/item/context": [
Expand Down
1 change: 1 addition & 0 deletions src/commands/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export const enum Commands {
config_display_language = 'i18n-ally.config-display-language',
config_source_language = 'i18n-ally.config-source-language',
config_locales = 'i18n-ally.config-locales',
go_to_range = 'i18n-ally.go-to-range',
set_display_language = 'i18n-ally.set-display-language',
set_source_language = 'i18n-ally.set-source-language',
extract_text = 'i18n-ally.extract-text',
Expand Down
44 changes: 22 additions & 22 deletions src/commands/deepl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,33 @@ import i18n from '~/i18n'
import { Config } from '~/core'
import { abbreviateNumber } from '~/utils'

const m: ExtensionModule = (ctx) => {
async function deepAuth() {
const apiKey = Config.deeplApiKey
async function deepAuth() {
const apiKey = Config.deeplApiKey

if (!apiKey) {
return window.showErrorMessage(
i18n.t('prompt.deepl_api_key_required'),
)
}
if (!apiKey) {
return window.showErrorMessage(
i18n.t('prompt.deepl_api_key_required'),
)
}

try {
const deeplUsage = await usage()
try {
const deeplUsage = await usage()

window.showInformationMessage(
i18n.t(
'prompt.deepl_usage',
abbreviateNumber(deeplUsage.character_count),
abbreviateNumber(deeplUsage.character_limit),
),
i18n.t('prompt.button_discard'),
)
}
catch (err) {
window.showErrorMessage(i18n.t('prompt.deepl_error_get_usage'))
}
window.showInformationMessage(
i18n.t(
'prompt.deepl_usage',
abbreviateNumber(deeplUsage.character_count),
abbreviateNumber(deeplUsage.character_limit),
),
i18n.t('prompt.button_discard'),
)
}
catch (err) {
window.showErrorMessage(i18n.t('prompt.deepl_error_get_usage'))
}
}

const m: ExtensionModule = (ctx) => {
return [
commands.registerCommand(Commands.deepl_usage, deepAuth),
]
Expand Down
11 changes: 8 additions & 3 deletions src/commands/detectHardStrings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,13 @@ export async function DetectHardStrings() {

for (const framework of frameworks) {
const temp = framework.detectHardStrings?.(doc)
if (temp && temp.length)
result.push(...temp)
if (temp && temp.length) {
result.push(...temp.map(i => ({
...i,
document: doc,
editor,
})))
}
}

editor.setDecorations(
Expand Down Expand Up @@ -70,7 +75,7 @@ export async function DetectHardStrings() {
.filter(Boolean),
)

window.showInformationMessage(result.map(i => i.text).join('\n'))
// window.showInformationMessage(result.map(i => i.text).join('\n'))

return result
}
Expand Down
21 changes: 21 additions & 0 deletions src/commands/gotoRange.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { commands, Range, Selection, TextEditor, TextEditorRevealType, window } from 'vscode'
import { Commands } from '~/extension'
import { ExtensionModule } from '~/modules'

export function GoToRange(editor: TextEditor, range: Range) {
if (editor && range) {
editor.selection = new Selection(
range.end,
range.start,
)
editor.revealRange(editor.selection, TextEditorRevealType.InCenter)
}
}

const m: ExtensionModule = () => {
return [
commands.registerCommand(Commands.go_to_range, GoToRange),
]
}

export default m
2 changes: 2 additions & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import refreshUsageReport from './refreshUsageReport'
import editor from './openEditor'
import review from './review'
import deepl from './deepl'
import gotoRange from './gotoRange'

const m: ExtensionModule = (ctx) => {
return flatten([
Expand All @@ -25,6 +26,7 @@ const m: ExtensionModule = (ctx) => {
editor(ctx),
review(ctx),
deepl(ctx),
gotoRange(ctx),
])
}

Expand Down
4 changes: 4 additions & 0 deletions src/extraction/parsers/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { TextDocument, TextEditor } from 'vscode'

export interface DetectionResult {
text: string
start: number
end: number
document?: TextDocument
editor?: TextEditor
isDynamic?: boolean
fullText?: string
fullStart?: number
Expand Down
8 changes: 6 additions & 2 deletions src/views/items/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { ExtensionContext, TreeItem } from 'vscode'
import { Config } from '~/core'

export abstract class BaseTreeItem extends TreeItem {
private _label = ''

constructor(
public readonly ctx: ExtensionContext,
) {
Expand All @@ -13,10 +15,12 @@ export abstract class BaseTreeItem extends TreeItem {
}

protected getLabel() {
return ''
return this._label
}

protected setLabel(value: string) {}
protected setLabel(value: string) {
this._label = value
}

// @ts-expect-error
get label() {
Expand Down
9 changes: 6 additions & 3 deletions src/views/items/CurrentFileExtractionItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import { BaseTreeItem } from './Base'
import { HardStringDetectResultItem } from './HardStringDetectResultItem'
import i18n from '~/i18n'
import { CurrentFile } from '~/core'
import { DetectionResult } from '~/extraction'

export class CurrentFileExtractionItem extends BaseTreeItem {
collapsibleState = TreeItemCollapsibleState.Collapsed
result: DetectionResult[] | undefined

constructor(readonly provider: CurrentFileLocalesTreeProvider) {
super(provider.ctx)
Expand All @@ -28,11 +30,12 @@ export class CurrentFileExtractionItem extends BaseTreeItem {
}

async getChildren() {
const strings = await CurrentFile.detectHardStrings()
if (!this.result)
this.result = await CurrentFile.detectHardStrings()

if (strings == null)
if (this.result == null)
return []

return strings.map(i => new HardStringDetectResultItem(this.ctx, i))
return this.result.map(i => new HardStringDetectResultItem(this.ctx, i))
}
}
5 changes: 4 additions & 1 deletion src/views/items/FeedbackItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ export class FeedbackItem extends BaseTreeItem {
constructor(ctx: ExtensionContext, define: FeedbackItemDefintion) {
super(ctx)
this.getLabel = () => define.text
this.iconPath = define.icon.startsWith('$') ? define.icon : this.getIcon(define.icon)
this.iconPath = define.icon.startsWith('$')
? define.icon
: this.getIcon(define.icon)

if (define.desc)
this.tooltip = define.desc
if (define.command) {
Expand Down
21 changes: 17 additions & 4 deletions src/views/items/HardStringDetectResultItem.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ExtensionContext, TreeItemCollapsibleState } from 'vscode'
import { ExtensionContext, Range, TreeItemCollapsibleState } from 'vscode'
import { BaseTreeItem } from './Base'
import { DetectionResult } from '~/extraction'
import { Commands } from '~/commands'

export class HardStringDetectResultItem extends BaseTreeItem {
collapsibleState = TreeItemCollapsibleState.None
Expand All @@ -10,9 +11,21 @@ export class HardStringDetectResultItem extends BaseTreeItem {
public readonly detection: DetectionResult,
) {
super(ctx)
}

getLabel() {
return this.detection.text.trim()
this.label = this.detection.text.trim()

if (this.detection.editor && this.detection.document) {
this.command = {
title: 'Go To',
command: Commands.go_to_range,
arguments: [
this.detection.editor,
new Range(
this.detection.document.positionAt(this.detection.fullStart ?? this.detection.start),
this.detection.document.positionAt(this.detection.fullEnd ?? this.detection.end),
),
],
}
}
}
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"resolveJsonModule": true,
"declaration": true,
"moduleResolution": "node",
"skipLibCheck": true,
"allowSyntheticDefaultImports": true,
"noUnusedLocals": true,
"paths": {
Expand Down

0 comments on commit 60c0f8a

Please sign in to comment.