Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Commit

Permalink
refactor: extract common tool command function
Browse files Browse the repository at this point in the history
  • Loading branch information
seanwu1105 committed Sep 10, 2023
1 parent 148e269 commit e86cb12
Show file tree
Hide file tree
Showing 19 changed files with 323 additions and 308 deletions.
1 change: 0 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
{ "ignore": [0, 1, -1], "ignoreDefaultValues": true }
],
"@typescript-eslint/explicit-module-boundary-types": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-unused-vars": [
"error",
{ "argsIgnorePattern": "^_" }
Expand Down
42 changes: 42 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@
"command": "qtForPython.compileTranslations",
"title": "Compile Qt Translation File (lrelease)",
"category": "Qt for Python"
},
{
"command": "qtForPython.formatQml",
"title": "Format QML File (qmlformat)",
"category": "Qt for Python"
}
],
"menus": {
Expand Down Expand Up @@ -186,6 +191,11 @@
"command": "qtForPython.compileTranslations",
"when": "resourceExtname == .ts && resourceLangId == xml",
"group": "qtForPython"
},
{
"command": "qtForPython.formatQml",
"when": "resourceLangId == qml",
"group": "qtForPython"
}
],
"explorer/context": [
Expand Down Expand Up @@ -228,6 +238,11 @@
"command": "qtForPython.compileTranslations",
"when": "resourceExtname == .ts && resourceLangId == xml",
"group": "qtForPython"
},
{
"command": "qtForPython.formatQml",
"when": "resourceLangId == qml",
"group": "qtForPython"
}
],
"editor/title": [
Expand Down Expand Up @@ -270,6 +285,11 @@
"command": "qtForPython.compileTranslations",
"when": "resourceExtname == .ts && resourceLangId == xml",
"group": "qtForPython"
},
{
"command": "qtForPython.formatQml",
"when": "resourceLangId == qml",
"group": "qtForPython"
}
],
"editor/context": [
Expand Down Expand Up @@ -312,6 +332,11 @@
"command": "qtForPython.compileTranslations",
"when": "resourceExtname == .ts && resourceLangId == xml",
"group": "qtForPython"
},
{
"command": "qtForPython.formatQml",
"when": "resourceLangId == qml",
"group": "qtForPython"
}
]
},
Expand Down Expand Up @@ -475,6 +500,23 @@
"default": [],
"markdownDescription": "The options passed to Qt `lrelease` executable. See [here](https://github.com/seanwu1105/vscode-qt-for-python#predefined-variables) for a detailed list of predefined variables.",
"scope": "resource"
},
"qtForPython.qmlformat.path": {
"type": "string",
"default": "",
"markdownDescription": "The path to `qmlformat` executable for QML formatting. Set to empty string to automatically resolve from the installed Python package. See [here](https://github.com/seanwu1105/vscode-qt-for-python#predefined-variables) for a detailed list of predefined variables.",
"scope": "resource"
},
"qtForPython.qmlformat.options": {
"type": "array",
"items": {
"type": "string"
},
"default": [
"--inplace"
],
"markdownDescription": "The options passed to `qmlformat` executable for QML formatting. See [here](https://github.com/seanwu1105/vscode-qt-for-python#predefined-variables) for a detailed list of predefined variables.",
"scope": "resource"
}
}
},
Expand Down
30 changes: 1 addition & 29 deletions src/commands.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { fromEventPattern } from 'rxjs'
import type { ExtensionContext } from 'vscode'
import { commands, Disposable, window } from 'vscode'
import { URI } from 'vscode-uri'
import { commands, Disposable } from 'vscode'
import { EXTENSION_NAMESPACE } from './constants'
import { createUi } from './designer/create-ui'
import { editUi } from './designer/edit-ui'
Expand All @@ -10,9 +9,7 @@ import { compileTranslations } from './lrelease/compile-translation'
import { extractTranslations } from './lupdate/extract-translation'
import { previewQml } from './qml/preview-qml'
import { compileResource } from './rcc/compile-resource'
import type { ErrorResult, SuccessResult } from './types'
import { compileUi } from './uic/compile-ui'
import { isNil } from './utils'

export function registerCommands$({ extensionUri }: RegisterCommandsArgs) {
return fromEventPattern<CommandCallbackValue>(
Expand Down Expand Up @@ -73,28 +70,3 @@ type CommandCallbackValue = Awaited<
>

export type CommandDeps = Pick<ExtensionContext, 'extensionUri'>

export function getTargetDocumentUri(
...args: readonly any[]
): GetTargetDocumentUriResult {
if (isNil(args[0])) {
const activeDocument = window.activeTextEditor?.document.uri

if (isNil(activeDocument))
return { kind: 'TypeError', message: 'No active document.' }

return { kind: 'Success', value: activeDocument }
}

if (!URI.isUri(args[0]))
return {
kind: 'TypeError',
message: `Invalid argument: ${JSON.stringify(args[0])}`,
}

return { kind: 'Success', value: args[0] }
}

export type GetTargetDocumentUriResult =
| SuccessResult<URI>
| ErrorResult<'Type'>
35 changes: 13 additions & 22 deletions src/designer/create-ui.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,31 @@
import * as path from 'node:path'
import { firstValueFrom } from 'rxjs'
import type { FileStat } from 'vscode'
import { FileType, workspace } from 'vscode'
import type { URI } from 'vscode-uri'
import type { CommandDeps } from '../commands'
import { getTargetDocumentUri } from '../commands'
import { run } from '../run'
import { getToolCommand$ } from '../tool-utils'
import { getToolCommandWithTargetDocumentUri } from '../tool-utils'
import type { ErrorResult, SuccessResult } from '../types'

export async function createUi({ extensionUri }: CommandDeps, ...args: any[]) {
const targetDocumentUriResult = getTargetDocumentUri(...args)

if (targetDocumentUriResult.kind !== 'Success') return targetDocumentUriResult

const uri = targetDocumentUriResult.value

const getToolCommandResult = await firstValueFrom(
getToolCommand$({
tool: 'designer',
extensionUri,
resource: uri,
}),
)
export async function createUi(
{ extensionUri }: CommandDeps,
...args: readonly unknown[]
) {
const result = await getToolCommandWithTargetDocumentUri({
extensionUri,
argsToGetTargetDocumentUri: args,
tool: 'designer',
})
if (result.kind !== 'Success') return result

if (getToolCommandResult.kind !== 'Success') return getToolCommandResult
const { command, options, uri } = result.value

const getDirectoryPathResult = await getDirectoryPath(uri)

if (getDirectoryPathResult.kind !== 'Success') return getDirectoryPathResult

return run({
command: [
...getToolCommandResult.value.command,
...getToolCommandResult.value.options,
],
command: [...command, ...options],
cwd: getDirectoryPathResult.value,
})
}
Expand Down
36 changes: 13 additions & 23 deletions src/designer/edit-ui.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,21 @@
import { firstValueFrom } from 'rxjs'
import type { CommandDeps } from '../commands'
import { getTargetDocumentUri } from '../commands'
import { run } from '../run'
import { getToolCommand$ } from '../tool-utils'
import { getToolCommandWithTargetDocumentUri } from '../tool-utils'

export async function editUi({ extensionUri }: CommandDeps, ...args: any[]) {
const targetDocumentUriResult = getTargetDocumentUri(...args)

if (targetDocumentUriResult.kind !== 'Success') return targetDocumentUriResult

const uiFile = targetDocumentUriResult.value

const getToolCommandResult = await firstValueFrom(
getToolCommand$({
tool: 'designer',
extensionUri,
resource: uiFile,
}),
)
export async function editUi(
{ extensionUri }: CommandDeps,
...args: readonly unknown[]
) {
const result = await getToolCommandWithTargetDocumentUri({
extensionUri,
argsToGetTargetDocumentUri: args,
tool: 'designer',
})
if (result.kind !== 'Success') return result

if (getToolCommandResult.kind !== 'Success') return getToolCommandResult
const { command, options, uri } = result.value

return run({
command: [
...getToolCommandResult.value.command,
...getToolCommandResult.value.options,
uiFile.fsPath,
],
command: [...command, ...options, uri.fsPath],
})
}
2 changes: 1 addition & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function onResultReceived(result: Result, outputChannel: OutputChannel) {
}

type Result =
| SuccessResult<any>
| SuccessResult<unknown>
| ExecError
| StdErrError
| ErrorResult<'Unexpected'>
Expand Down
35 changes: 10 additions & 25 deletions src/linguist/edit-translations.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,19 @@
import { firstValueFrom } from 'rxjs'
import type { CommandDeps } from '../commands'
import { getTargetDocumentUri } from '../commands'
import { run } from '../run'
import { getToolCommand$ } from '../tool-utils'
import { getToolCommandWithTargetDocumentUri } from '../tool-utils'

export async function editTranslations(
{ extensionUri }: CommandDeps,
...args: any[]
...args: readonly unknown[]
) {
const targetDocumentUriResult = getTargetDocumentUri(...args)

if (targetDocumentUriResult.kind !== 'Success') return targetDocumentUriResult

const translationFile = targetDocumentUriResult.value

const getToolCommandResult = await firstValueFrom(
getToolCommand$({
tool: 'linguist',
extensionUri,
resource: translationFile,
}),
)
const result = await getToolCommandWithTargetDocumentUri({
extensionUri,
argsToGetTargetDocumentUri: args,
tool: 'linguist',
})
if (result.kind !== 'Success') return result

if (getToolCommandResult.kind !== 'Success') return getToolCommandResult
const { command, options, uri } = result.value

return run({
command: [
...getToolCommandResult.value.command,
...getToolCommandResult.value.options,
translationFile.fsPath,
],
})
return run({ command: [...command, ...options, uri.fsPath] })
}
35 changes: 10 additions & 25 deletions src/lrelease/compile-translation.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,19 @@
import { firstValueFrom } from 'rxjs'
import type { CommandDeps } from '../commands'
import { getTargetDocumentUri } from '../commands'
import { run } from '../run'
import { getToolCommand$ } from '../tool-utils'
import { getToolCommandWithTargetDocumentUri } from '../tool-utils'

export async function compileTranslations(
{ extensionUri }: CommandDeps,
...args: any[]
...args: readonly unknown[]
) {
const targetDocumentUriResult = getTargetDocumentUri(...args)

if (targetDocumentUriResult.kind !== 'Success') return targetDocumentUriResult

const translationFile = targetDocumentUriResult.value

const getToolCommandResult = await firstValueFrom(
getToolCommand$({
tool: 'lrelease',
extensionUri,
resource: translationFile,
}),
)
const result = await getToolCommandWithTargetDocumentUri({
extensionUri,
argsToGetTargetDocumentUri: args,
tool: 'lrelease',
})
if (result.kind !== 'Success') return result

if (getToolCommandResult.kind !== 'Success') return getToolCommandResult
const { command, options, uri } = result.value

return run({
command: [
...getToolCommandResult.value.command,
translationFile.fsPath,
...getToolCommandResult.value.options,
],
})
return run({ command: [...command, uri.fsPath, ...options] })
}
35 changes: 10 additions & 25 deletions src/lupdate/extract-translation.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,19 @@
import { firstValueFrom } from 'rxjs'
import type { CommandDeps } from '../commands'
import { getTargetDocumentUri } from '../commands'
import { run } from '../run'
import { getToolCommand$ } from '../tool-utils'
import { getToolCommandWithTargetDocumentUri } from '../tool-utils'

export async function extractTranslations(
{ extensionUri }: CommandDeps,
...args: any[]
...args: readonly unknown[]
) {
const targetDocumentUriResult = getTargetDocumentUri(...args)

if (targetDocumentUriResult.kind !== 'Success') return targetDocumentUriResult

const sourceFile = targetDocumentUriResult.value

const getToolCommandResult = await firstValueFrom(
getToolCommand$({
tool: 'lupdate',
extensionUri,
resource: sourceFile,
}),
)
const result = await getToolCommandWithTargetDocumentUri({
extensionUri,
argsToGetTargetDocumentUri: args,
tool: 'lupdate',
})
if (result.kind !== 'Success') return result

if (getToolCommandResult.kind !== 'Success') return getToolCommandResult
const { command, options, uri } = result.value

return run({
command: [
...getToolCommandResult.value.command,
sourceFile.fsPath,
...getToolCommandResult.value.options,
],
})
return run({ command: [...command, uri.fsPath, ...options] })
}
Loading

0 comments on commit e86cb12

Please sign in to comment.