Skip to content

Commit

Permalink
feat: new command get next usage, close #636
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Aug 12, 2021
1 parent c976cdb commit ea1b386
Show file tree
Hide file tree
Showing 19 changed files with 70 additions and 2 deletions.
1 change: 1 addition & 0 deletions locales/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"command.extract.disable-auto-detect": "Deaktivieren der automatisierten Erkennung",
"command.extract.enable-auto-detect": "Aktivieren der automatisierten Erkennung",
"command.fulfill_keys": "Vervollständige Schlüssel mit leeren Texten",
"command.go_to_next_usage": "",
"command.go_to_range": "Springe zum Bereich",
"command.insert_key": "Schlüssel einfügen",
"command.locale_visibility_hide": "Lokalisierung verbergen",
Expand Down
1 change: 1 addition & 0 deletions locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"command.extract.disable-auto-detect": "Disable hard-coded strings auto detection",
"command.extract.enable-auto-detect": "Enable hard-coded strings auto detection",
"command.fulfill_keys": "Fulfill keys with empty strings",
"command.go_to_next_usage": "Go to next usage",
"command.go_to_range": "Go to range",
"command.insert_key": "Insert key",
"command.locale_visibility_hide": "Hide locale",
Expand Down
1 change: 1 addition & 0 deletions locales/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"command.extract.disable-auto-detect": "",
"command.extract.enable-auto-detect": "",
"command.fulfill_keys": "Rellenar claves con cadena vacía",
"command.go_to_next_usage": "",
"command.go_to_range": "",
"command.insert_key": "Insertar clave",
"command.locale_visibility_hide": "Ocultar configuración regional",
Expand Down
1 change: 1 addition & 0 deletions locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"command.extract.disable-auto-detect": "",
"command.extract.enable-auto-detect": "",
"command.fulfill_keys": "Remplir les champs vides de Clés",
"command.go_to_next_usage": "",
"command.go_to_range": "Aller à la plage",
"command.insert_key": "Insérer une clé",
"command.locale_visibility_hide": "Cacher le dossier de la locale",
Expand Down
1 change: 1 addition & 0 deletions locales/ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"command.extract.disable-auto-detect": "",
"command.extract.enable-auto-detect": "",
"command.fulfill_keys": "空文字でキーを埋める",
"command.go_to_next_usage": "",
"command.go_to_range": "",
"command.insert_key": "",
"command.locale_visibility_hide": "ロケールを隠す",
Expand Down
1 change: 1 addition & 0 deletions locales/nb-NO.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"command.extract.disable-auto-detect": "",
"command.extract.enable-auto-detect": "",
"command.fulfill_keys": "",
"command.go_to_next_usage": "",
"command.go_to_range": "",
"command.insert_key": "",
"command.locale_visibility_hide": "Skjul oversettelse",
Expand Down
1 change: 1 addition & 0 deletions locales/nl-NL.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"command.extract.disable-auto-detect": "",
"command.extract.enable-auto-detect": "",
"command.fulfill_keys": "Sleutels met lege waarden aanvullen",
"command.go_to_next_usage": "",
"command.go_to_range": "",
"command.insert_key": "",
"command.locale_visibility_hide": "Verberg locale",
Expand Down
1 change: 1 addition & 0 deletions locales/pt-BR.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"command.extract.disable-auto-detect": "",
"command.extract.enable-auto-detect": "",
"command.fulfill_keys": "Preencher chaves com strings vazias",
"command.go_to_next_usage": "",
"command.go_to_range": "",
"command.insert_key": "",
"command.locale_visibility_hide": "Esconder locale",
Expand Down
1 change: 1 addition & 0 deletions locales/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"command.extract.disable-auto-detect": "",
"command.extract.enable-auto-detect": "",
"command.fulfill_keys": "Заполнять ключ пустой строкой",
"command.go_to_next_usage": "",
"command.go_to_range": "",
"command.insert_key": "Вставить ключ",
"command.locale_visibility_hide": "Скрыть перевод",
Expand Down
1 change: 1 addition & 0 deletions locales/sv-SE.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"command.extract.disable-auto-detect": "",
"command.extract.enable-auto-detect": "",
"command.fulfill_keys": "Skapa saknade nycklar med tomma strängar",
"command.go_to_next_usage": "",
"command.go_to_range": "",
"command.insert_key": "Lägg till nyckel",
"command.locale_visibility_hide": "Göm locale",
Expand Down
1 change: 1 addition & 0 deletions locales/tr.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"command.extract.disable-auto-detect": "",
"command.extract.enable-auto-detect": "",
"command.fulfill_keys": "Boş bırakılmış çevirileri doldur",
"command.go_to_next_usage": "",
"command.go_to_range": "Aralığa git",
"command.insert_key": "Çeviri ekle",
"command.locale_visibility_hide": "Dili gizle",
Expand Down
1 change: 1 addition & 0 deletions locales/uk-UA.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"command.extract.disable-auto-detect": "",
"command.extract.enable-auto-detect": "",
"command.fulfill_keys": "Заповнювати ключ порожній рядком",
"command.go_to_next_usage": "",
"command.go_to_range": "",
"command.insert_key": "Вставити ключ",
"command.locale_visibility_hide": "Приховати мову",
Expand Down
1 change: 1 addition & 0 deletions locales/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"command.extract.disable-auto-detect": "",
"command.extract.enable-auto-detect": "",
"command.fulfill_keys": "使用空字符串填充文案路径",
"command.go_to_next_usage": "",
"command.go_to_range": "",
"command.insert_key": "插入i18n文案",
"command.locale_visibility_hide": "隐藏此语言",
Expand Down
1 change: 1 addition & 0 deletions locales/zh-TW.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"command.extract.disable-auto-detect": "",
"command.extract.enable-auto-detect": "",
"command.fulfill_keys": "使用空字串填充文案路徑",
"command.go_to_next_usage": "",
"command.go_to_range": "",
"command.insert_key": "插入i18n文案",
"command.locale_visibility_hide": "隱藏此語言",
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,11 @@
"category": "%extname%",
"title": "%command.go_to_range%"
},
{
"command": "i18n-ally.go-to-next-usage",
"category": "%extname%",
"title": "%command.go_to_next_usage%"
},
{
"command": "i18n-ally.open-docs-hard-string",
"category": "%extname%",
Expand Down
1 change: 1 addition & 0 deletions src/commands/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export const enum Commands {
config_source_language = 'i18n-ally.config-source-language',
config_locales = 'i18n-ally.config-locales',
go_to_range = 'i18n-ally.go-to-range',
go_to_next_usage = 'i18n-ally.go-to-next-usage',
set_display_language = 'i18n-ally.set-display-language',
set_source_language = 'i18n-ally.set-source-language',
extract_text = 'i18n-ally.extract-text',
Expand Down
46 changes: 46 additions & 0 deletions src/commands/gotoNextUsage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { commands, Range, window } from 'vscode'
import { GoToRange } from './gotoRange'
import { CurrentFile, KeyDetector, KeyInDocument } from '~/core'
import { Commands } from '~/extension'
import { ExtensionModule } from '~/modules'

export function GoToNextUsage() {
const editor = window.activeTextEditor
const document = editor?.document
if (!editor || !document)
return

const usages = KeyDetector.getUsages(document, CurrentFile.loader)
if (!usages?.keys.length)
return

const current = document.offsetAt(editor.selection.start)

function getDiff(i: KeyInDocument) {
const diff = current - i.start
if (diff < 0)
return Infinity
return diff
}

const last = Array.from(usages.keys).sort((a, b) => getDiff(a) - getDiff(b))[0]

if (!last)
return

const index = usages.keys.indexOf(last)
const next = (index + 1) % usages.keys.length
const nextKey = usages.keys[next]

if (!nextKey)
return

const range = new Range(document.positionAt(nextKey.start), document.positionAt(nextKey.end))
GoToRange(document, range)
}

export default <ExtensionModule> function() {
return [
commands.registerCommand(Commands.go_to_next_usage, GoToNextUsage),
]
}
2 changes: 2 additions & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import editor from './openEditor'
import review from './review'
import deepl from './deepl'
import gotoRange from './gotoRange'
import gotoNextUsage from './gotoNextUsage'
import batchHardStringsExtract from './extractStringBulk'
import { ExtensionModule } from '~/modules'

Expand All @@ -27,6 +28,7 @@ const m: ExtensionModule = (ctx) => {
review(ctx),
deepl(ctx),
gotoRange(ctx),
gotoNextUsage(ctx),
])
}

Expand Down
4 changes: 2 additions & 2 deletions src/editor/definition.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { languages, TextDocument, Position, CancellationToken, Definition, DefinitionLink, Uri, workspace, Location, Range } from 'vscode'
import { languages, TextDocument, Position, Definition, DefinitionLink, Uri, workspace, Location, Range } from 'vscode'
import { ExtensionModule } from '~/modules'
import { Global, KeyDetector, CurrentFile } from '~/core'

class DefinitionProvider {
async provideDefinition(document: TextDocument, position: Position, token: CancellationToken): Promise<Definition | DefinitionLink[]> {
async provideDefinition(document: TextDocument, position: Position): Promise<Definition | DefinitionLink[]> {
const key = KeyDetector.getKey(document, position)
if (!key)
return []
Expand Down

0 comments on commit ea1b386

Please sign in to comment.