Skip to content

Commit

Permalink
wip: support i18n-ally.annotationRange
Browse files Browse the repository at this point in the history
  • Loading branch information
ipcjs committed Dec 13, 2023
1 parent a0b9264 commit 4622f5c
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 6 deletions.
9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,15 @@
"default": true,
"description": "%config.annotation_in_place%"
},
"i18n-ally.annotationRange" : {
"type": "string",
"default": "key",
"enum": [
"key",
"annotation"
],
"description": "注释的范围"
},
"i18n-ally.annotationMaxLength": {
"type": "number",
"default": 40,
Expand Down
4 changes: 4 additions & 0 deletions src/core/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ export class Config {
return this.getConfig<boolean>('annotationInPlace') ?? true
}

static get annotationRange(): 'key' | 'annotation' {
return this.getConfig('annotationRange') ?? 'key'
}

static get namespace(): boolean | undefined {
return this.getConfig<boolean>('namespace')
}
Expand Down
2 changes: 2 additions & 0 deletions src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ export interface KeyInDocument {
end: number
key: string
quoted: boolean
annotationStart?: number
annotationEnd?: number
}

export interface KeyOccurrence {
Expand Down
8 changes: 5 additions & 3 deletions src/editor/annotation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ const annotation: ExtensionModule = (ctx) => {
const sourceLanguage = Config.sourceLanguage
const showAnnotations = Config.annotations
const annotationInPlace = Config.annotationInPlace
const annotationRange = Config.annotationRange
const themeAnnotationMissing = Config.themeAnnotationMissing
const themeAnnotation = Config.themeAnnotation
const themeAnnotationBorder = Config.themeAnnotationBorder
Expand All @@ -105,11 +106,12 @@ const annotation: ExtensionModule = (ctx) => {
? `${namespace}.${key.key}`
: key.key

const isUseAnnotation = annotationRange === 'annotation' && key.annotationStart != null && key.annotationEnd != null
const range = new Range(
document.positionAt(key.start),
document.positionAt(key.end),
document.positionAt(isUseAnnotation ? key.annotationStart! : key.start),
document.positionAt(isUseAnnotation ? key.annotationEnd! : key.end),
)
const rangeWithQuotes = key.quoted
const rangeWithQuotes = !isUseAnnotation && key.quoted
? new Range(
range.start.with(undefined, range.start.character - 1),
range.end.with(undefined, range.end.character + 1),
Expand Down
4 changes: 2 additions & 2 deletions src/frameworks/flutter-l10n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class FlutterL10nFramework extends Framework {

// for visualize the regex, you can use https://regexper.com/
usageMatchRegex = [
'S\\.of\\([\\w.]+\\)[?!]?\\.({key})\\W',
'AppLocalizations\\.of\\([\\w.]+\\)[?!]?\\.({key})\\W',
'(?<annotation>S\\.of\\([\\w.]+\\)[?!]?\\.(?<key>{key}))\\W',
'(?<annotation>AppLocalizations\\.of\\([\\w.]+\\)[?!]?\\.(?<key>{key}))\\W',
]

preferredKeystyle?: KeyStyle = 'flat'
Expand Down
7 changes: 6 additions & 1 deletion src/utils/Regex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@ export function handleRegexMatch(
starts: number[] = [],
): KeyInDocument | undefined {
const matchString = match[0]
let key = match[1]
let key = match.groups?.key ?? match[1]
if (!key)
return

const start = match.index + matchString.lastIndexOf(key)
const end = start + key.length
const scope = scopes.find(s => s.start <= start && s.end >= end)
const quoted = QUOTE_SYMBOLS.includes(text[start - 1])
const annotation = match.groups?.annotation ?? match[0]
const annotationStart = match.index + matchString.lastIndexOf(annotation)
const annotationEnd = annotationStart + annotation.length

const namespace = scope?.namespace || defaultNamespace

Expand All @@ -50,6 +53,8 @@ export function handleRegexMatch(
start,
end,
quoted,
annotationStart,
annotationEnd,
}
}
}
Expand Down

0 comments on commit 4622f5c

Please sign in to comment.