Skip to content

Commit

Permalink
Adding new getScopeRange option to the custom framework
Browse files Browse the repository at this point in the history
  • Loading branch information
aranard-thetradedesk committed Dec 13, 2022
1 parent 738536f commit 899fe37
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions src/frameworks/custom.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import path from 'path'
import fs from 'fs'
import { workspace, FileSystemWatcher } from 'vscode'
import { workspace, FileSystemWatcher, TextDocument } from 'vscode'
import YAML from 'js-yaml'
import { Framework } from './base'
import { Framework, ScopeRange } from './base'
import { Global } from '~/core'
import { LanguageId, File, Log } from '~/utils'

Expand All @@ -11,6 +11,7 @@ const CustomFrameworkConfigFilename = './.vscode/i18n-ally-custom-framework.yml'
interface CustomFrameworkConfig {
languageIds?: LanguageId[] | LanguageId
usageMatchRegex?: string[] | string
namespaceMatchRegex?: string
refactorTemplates?: string[]
monopoly?: boolean

Expand Down Expand Up @@ -81,6 +82,38 @@ class CustomFramework extends Framework {
.map(i => i.replace(/\$1/g, keypath))
}

getScopeRange(document: TextDocument): ScopeRange[] | undefined {
if (!this.data?.namespaceMatchRegex)
return undefined

if (!this.languageIds.includes(document.languageId as any))
return

const ranges: ScopeRange[] = []
const text = document.getText()
const reg = new RegExp(this.data.namespaceMatchRegex, 'g')

for (const match of text.matchAll(reg)) {
if (match?.index == null)
continue

// end previous scope
if (ranges.length)
ranges[ranges.length - 1].end = match.index

// start new scope if namespace provides
if (match[1]) {
ranges.push({
start: match.index,
end: text.length,
namespace: match[1] as string,
})
}
}

return ranges
}

startWatch(root?: string) {
if (this.watchingFor) {
this.watchingFor = undefined
Expand Down

0 comments on commit 899fe37

Please sign in to comment.