Skip to content

Commit

Permalink
feat: add linguist support
Browse files Browse the repository at this point in the history
Fix #282
  • Loading branch information
seanwu1105 committed Mar 4, 2023
1 parent adf7eb1 commit ba9e29f
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 14 deletions.
35 changes: 30 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,14 @@
"category": "Qt for Python"
},
{
"command": "qtForPython.lupdate",
"command": "qtForPython.extractTranslations",
"title": "Extract to Qt Translation File (lupdate)",
"category": "Qt for Python"
},
{
"command": "qtForPython.editTranslations",
"title": "Edit Qt Translation File (linguist)",
"category": "Qt for Python"
}
],
"menus": {
Expand Down Expand Up @@ -163,9 +168,14 @@
"group": "qtForPython"
},
{
"command": "qtForPython.lupdate",
"command": "qtForPython.extractTranslations",
"when": "resourceLangId == python || resourceLangId == qml || resourceLangId == xml && resourceExtname == .ui",
"group": "qtForPython"
},
{
"command": "qtForPython.editTranslations",
"when": "resourceExtname == .ts && resourceLangId == xml",
"group": "qtForPython"
}
],
"explorer/context": [
Expand Down Expand Up @@ -195,9 +205,14 @@
"group": "qtForPython"
},
{
"command": "qtForPython.lupdate",
"command": "qtForPython.extractTranslations",
"when": "resourceLangId == python || resourceLangId == qml || resourceLangId == xml && resourceExtname == .ui",
"group": "qtForPython"
},
{
"command": "qtForPython.editTranslations",
"when": "resourceExtname == .ts && resourceLangId == xml",
"group": "qtForPython"
}
],
"editor/title": [
Expand Down Expand Up @@ -227,9 +242,14 @@
"group": "qtForPython"
},
{
"command": "qtForPython.lupdate",
"command": "qtForPython.extractTranslations",
"when": "resourceLangId == python || resourceLangId == qml || resourceLangId == xml && resourceExtname == .ui",
"group": "qtForPython"
},
{
"command": "qtForPython.editTranslations",
"when": "resourceExtname == .ts && resourceLangId == xml",
"group": "qtForPython"
}
],
"editor/context": [
Expand Down Expand Up @@ -259,9 +279,14 @@
"group": "qtForPython"
},
{
"command": "qtForPython.lupdate",
"command": "qtForPython.extractTranslations",
"when": "resourceLangId == python || resourceLangId == qml || resourceLangId == xml && resourceExtname == .ui",
"group": "qtForPython"
},
{
"command": "qtForPython.editTranslations",
"when": "resourceExtname == .ts && resourceLangId == xml",
"group": "qtForPython"
}
]
},
Expand Down
1 change: 1 addition & 0 deletions python/.coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ omit =
scripts/designer.py
scripts/qml.py
scripts/lupdate.py
scripts/linguist.py

[report]
fail_under = 100
Expand Down
17 changes: 17 additions & 0 deletions python/scripts/linguist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# pylint: disable=import-error,ungrouped-imports

import sys

from utils import is_installed, parse_qt_dependency

if __name__ == "__main__":
dep = parse_qt_dependency()
if dep == "PySide6":
from PySide6.scripts.pyside_tool import linguist

elif is_installed("PySide6"):
from PySide6.scripts.pyside_tool import linguist
else:
sys.exit("No Qt Linguist can be found in current Python environment.")

sys.exit(linguist())
2 changes: 1 addition & 1 deletion python/scripts/qmlls.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# pylint: disable=import-error
# pylint: disable=import-error,ungrouped-imports

import sys

Expand Down
4 changes: 3 additions & 1 deletion python/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

ASSETS_DIR = os.path.join(TESTS_DIR, "assets")

SupportedScripts = typing.Literal["designer", "lupdate", "qml", "qmlls", "rcc", "uic"]
SupportedScripts = typing.Literal[
"designer", "qml", "qmlls", "rcc", "uic", "lupdate", "linguist"
]


def filter_available_qt_dependencies(
Expand Down
14 changes: 14 additions & 0 deletions python/tests/test_linguist.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import pytest

from scripts.utils import SupportedQtDependencies
from tests import filter_available_qt_dependencies, invoke_script


@pytest.mark.skip(reason="a GUI app cannot be closed gracefully")
@pytest.mark.parametrize(
"qt_dependency",
filter_available_qt_dependencies(["PySide6"]),
)
def test_qml_help(qt_dependency: SupportedQtDependencies):
result = invoke_script("linguist", [], qt_dependency)
assert result.returncode == 0
11 changes: 8 additions & 3 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { URI } from 'vscode-uri'
import { EXTENSION_NAMESPACE } from './constants'
import { createUi } from './designer/create-ui'
import { editUi } from './designer/edit-ui'
import { lupdate } from './lupdate/lupdate'
import { editTranslations } from './linguist/edit-translations'
import { extractTranslations } from './lupdate/extract-translation'
import { previewQml } from './qml/preview-qml'
import { compileResource } from './rcc/compile-resource'
import type { ErrorResult, SuccessResult } from './types'
Expand Down Expand Up @@ -53,8 +54,12 @@ const COMMANDS = [
callback: previewQml,
},
{
name: 'lupdate',
callback: lupdate,
name: 'extractTranslations',
callback: extractTranslations,
},
{
name: 'editTranslations',
callback: editTranslations,
},
] as const

Expand Down
34 changes: 34 additions & 0 deletions src/linguist/edit-translations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { firstValueFrom } from 'rxjs'
import type { CommandDeps } from '../commands'
import { getTargetDocumentUri } from '../commands'
import { run } from '../run'
import { getToolCommand$ } from '../tool-utils'

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

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

const translationFile = targetDocumentUriResult.value

const getToolCommandResult = await firstValueFrom(
getToolCommand$({
tool: 'linguist',
extensionUri,
resource: translationFile,
}),
)

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

return run({
command: [
...getToolCommandResult.value.command,
...getToolCommandResult.value.options,
translationFile.fsPath,
],
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { getTargetDocumentUri } from '../commands'
import { run } from '../run'
import { getToolCommand$ } from '../tool-utils'

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

if (targetDocumentUriResult.kind !== 'Success') return targetDocumentUriResult
Expand Down
6 changes: 4 additions & 2 deletions src/test/suite/linguist/e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ suite('linguist/e2e', () => {
await removeGeneratedFile(sampleFilenameNoExt)
})

test('should run lupdate command', async () => {
await commands.executeCommand(`${EXTENSION_NAMESPACE}.lupdate`)
test('should run extractTranslations command', async () => {
await commands.executeCommand(
`${EXTENSION_NAMESPACE}.extractTranslations`,
)

return waitFor(async () => {
const readResult = await workspace.fs.readFile(
Expand Down
2 changes: 1 addition & 1 deletion src/test/suite/linguist/lupdate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ suite('lupdate', () => {
test('should include the command', async () =>
assert.ok(
(await commands.getCommands(true)).includes(
`${EXTENSION_NAMESPACE}.lupdate`,
`${EXTENSION_NAMESPACE}.extractTranslations`,
),
))
})
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type SupportedTool =
| 'designer'
| 'qml'
| 'lupdate'
| 'linguist'

export type SuccessResult<T, Name extends string = ''> = {
readonly kind: `${Name}Success`
Expand Down

0 comments on commit ba9e29f

Please sign in to comment.