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

Commit

Permalink
refactor: replace extensionPath with extensionUri
Browse files Browse the repository at this point in the history
  • Loading branch information
seanwu1105 committed Jan 29, 2023
1 parent cddfcf3 commit c35d822
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 42 deletions.
2 changes: 1 addition & 1 deletion src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const COMMANDS = [
},
] as const

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

export function getTargetDocumentUri(
...args: readonly any[]
Expand Down
4 changes: 2 additions & 2 deletions src/designer/create-ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type { ErrorResult, SuccessResult } from '../types'
import { notNil } from '../utils'

export async function createUi(
{ extensionPath }: CommandDeps,
{ extensionUri }: CommandDeps,
...args: any[]
): Promise<CreateUiResult> {
const targetDocumentUriResult = getTargetDocumentUri(...args)
Expand All @@ -21,7 +21,7 @@ export async function createUi(

const getToolCommandResult = await getToolCommand({
tool: 'designer',
extensionPath,
extensionUri,
resource: uri,
})

Expand Down
4 changes: 2 additions & 2 deletions src/designer/edit-ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { getToolCommand } from '../tool-utils'
import type { ErrorResult, SuccessResult } from '../types'

export async function editUi(
{ extensionPath }: CommandDeps,
{ extensionUri }: CommandDeps,
...args: any[]
): Promise<EditUiResult> {
const targetDocumentUriResult = getTargetDocumentUri(...args)
Expand All @@ -17,7 +17,7 @@ export async function editUi(

const getToolCommandResult = await getToolCommand({
tool: 'designer',
extensionPath,
extensionUri,
resource: uiFile,
})

Expand Down
8 changes: 4 additions & 4 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,27 @@ export async function activate(context: ExtensionContext) {

registerUicLiveExecution({
subscriptions: context.subscriptions,
extensionPath: context.extensionPath,
extensionUri: context.extensionUri,
onResultReceived,
})

registerQmlLanguageServer({
subscriptions: context.subscriptions,
extensionPath: context.extensionPath,
extensionUri: context.extensionUri,
outputChannel,
onResult: onResultReceived,
})
}

function registerCommands({ extensionPath, subscriptions }: ExtensionContext) {
function registerCommands({ extensionUri, subscriptions }: ExtensionContext) {
return COMMANDS.map(command =>
subscriptions.push(
commands.registerCommand(
`${EXTENSION_NAMESPACE}.${command.name}`,
async (...args) => {
try {
return onResultReceived(
await command.callback({ extensionPath }, ...args),
await command.callback({ extensionUri }, ...args),
)
} catch (e) {
return onResultReceived({
Expand Down
6 changes: 3 additions & 3 deletions src/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { notNil } from './utils'

export async function resolveScriptCommand({
tool,
extensionPath,
extensionUri,
resource,
}: ResolveScriptCommandArgs): Promise<ResolveScriptCommandResult> {
const pythonInterpreterPathResult = await getPythonInterpreterPath(resource)
Expand All @@ -16,7 +16,7 @@ export async function resolveScriptCommand({
kind: 'Success',
value: [
...pythonInterpreterPathResult.value,
path.join(extensionPath, 'python', 'scripts', `${tool}.py`),
path.join(extensionUri.fsPath, 'python', 'scripts', `${tool}.py`),
],
}
}
Expand All @@ -26,7 +26,7 @@ export async function resolveScriptCommand({

export type ResolveScriptCommandArgs = {
readonly tool: SupportedTool
readonly extensionPath: string
readonly extensionUri: URI
readonly resource: URI | undefined
}

Expand Down
13 changes: 7 additions & 6 deletions src/qmlls/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import {
LanguageClient,
RevealOutputChannelOn,
} from 'vscode-languageclient/node'
import type { URI } from 'vscode-uri'
import { wrapAndJoinCommandArgsWithQuotes } from '../run'
import { getToolCommand } from '../tool-utils'
import type { ErrorResult, SuccessResult } from '../types'
import { withConcatMap } from '../utils'

export async function registerQmlLanguageServer({
subscriptions,
extensionPath,
extensionUri,
outputChannel,
onResult,
}: RegisterQmlLanguageServerArgs) {
Expand Down Expand Up @@ -43,7 +44,7 @@ export async function registerQmlLanguageServer({
await stopClient()

const startClientResult = await startClient({
extensionPath,
extensionUri,
outputChannel,
})

Expand All @@ -60,18 +61,18 @@ export async function registerQmlLanguageServer({

type RegisterQmlLanguageServerArgs = {
readonly subscriptions: Disposable[]
readonly extensionPath: string
readonly extensionUri: URI
readonly outputChannel: OutputChannel
readonly onResult: (result: StartClientResult) => void
}

async function startClient({
extensionPath,
extensionUri,
outputChannel,
}: StartClientArgs): Promise<StartClientResult> {
const getToolCommandResult = await getToolCommand({
tool: 'qmlls',
extensionPath,
extensionUri,
resource: undefined,
})

Expand Down Expand Up @@ -105,7 +106,7 @@ async function startClient({
}

type StartClientArgs = {
readonly extensionPath: string
readonly extensionUri: URI
readonly outputChannel: OutputChannel
}

Expand Down
4 changes: 2 additions & 2 deletions src/rcc/compile-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { getToolCommand } from '../tool-utils'
import type { ErrorResult, SuccessResult } from '../types'

export async function compileResource(
{ extensionPath }: CommandDeps,
{ extensionUri }: CommandDeps,
...args: any[]
): Promise<CompileResourceResult> {
const targetDocumentUriResult = getTargetDocumentUri(...args)
Expand All @@ -17,7 +17,7 @@ export async function compileResource(

const getToolCommandResult = await getToolCommand({
tool: 'rcc',
extensionPath,
extensionUri,
resource: qrcFile,
})

Expand Down
6 changes: 3 additions & 3 deletions src/test/suite/python.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ suite('python', () => {
suite('resolveScriptCommand', () => {
const args: ResolveScriptCommandArgs = {
tool: 'qmlls',
extensionPath: 'xyz',
extensionUri: URI.file('file:///xyz'),
resource: URI.file('file:///abc'),
}

Expand Down Expand Up @@ -50,7 +50,7 @@ suite('python', () => {
value: [
...mockPythonExecCommand,
path.join(
args.extensionPath,
args.extensionUri.fsPath,
'python',
'scripts',
`${args.tool}.py`,
Expand Down Expand Up @@ -83,7 +83,7 @@ suite('python', () => {
value: [
mockDefaultInterpreterPath,
path.join(
args.extensionPath,
args.extensionUri.fsPath,
'python',
'scripts',
`${args.tool}.py`,
Expand Down
6 changes: 3 additions & 3 deletions src/test/suite/tool-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ suite('tool-utils', () => {

result = await getToolCommand({
tool: 'rcc',
extensionPath: '',
extensionUri: URI.file('file:///xyz'),
resource: mockResource,
})
})
Expand Down Expand Up @@ -67,7 +67,7 @@ suite('tool-utils', () => {
test('should return path with options', async () => {
result = await getToolCommand({
tool: 'rcc',
extensionPath: '',
extensionUri: URI.file('file:///xyz'),
resource: mockResource,
})

Expand All @@ -90,7 +90,7 @@ suite('tool-utils', () => {
test('should return path with options', async () => {
result = await getToolCommand({
tool: 'rcc',
extensionPath: '',
extensionUri: URI.file('file:///xyz'),
resource: mockResource,
})

Expand Down
6 changes: 3 additions & 3 deletions src/tool-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { ErrorResult, SuccessResult, SupportedTool } from './types'

export async function getToolCommand({
tool,
extensionPath,
extensionUri,
resource,
}: GetToolCommandArgs): Promise<GetToolCommandResult> {
const configToolOptions = getOptionsFromConfig({ tool, resource })
Expand All @@ -21,7 +21,7 @@ export async function getToolCommand({

const resolveScriptCommandResult = await resolveScriptCommand({
tool,
extensionPath,
extensionUri,
resource,
})

Expand All @@ -39,7 +39,7 @@ export async function getToolCommand({

type GetToolCommandArgs = {
readonly tool: SupportedTool
readonly extensionPath: string
readonly extensionUri: URI
readonly resource: URI | undefined
}

Expand Down
4 changes: 2 additions & 2 deletions src/uic/compile-ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { getToolCommand } from '../tool-utils'
import type { ErrorResult, SuccessResult } from '../types'

export async function compileUi(
{ extensionPath }: CommandDeps,
{ extensionUri }: CommandDeps,
...args: any[]
): Promise<CompileUiResult> {
const targetDocumentUriResult = getTargetDocumentUri(...args)
Expand All @@ -17,7 +17,7 @@ export async function compileUi(

const getToolCommandResult = await getToolCommand({
tool: 'uic',
extensionPath,
extensionUri,
resource: uiFile,
})

Expand Down
18 changes: 7 additions & 11 deletions src/uic/uic-live-execution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,30 @@ import type { ErrorResult, SuccessResult } from '../types'
import { compileUi } from './compile-ui'

export function registerUicLiveExecution({
extensionPath,
extensionUri,
subscriptions,
onResultReceived,
}: RegisterUicLiveExecutionArgs) {
const watcher = workspace.createFileSystemWatcher('**/*.ui')
watcher.onDidChange(async uri =>
onResultReceived(
await onUiFileUpdated({ uri, extensionPath: extensionPath }),
),
onResultReceived(await onUiFileUpdated({ uri, extensionUri })),
)
watcher.onDidCreate(async uri =>
onResultReceived(
await onUiFileUpdated({ uri, extensionPath: extensionPath }),
),
onResultReceived(await onUiFileUpdated({ uri, extensionUri })),
)
subscriptions.push(watcher)
}

type RegisterUicLiveExecutionArgs = Pick<
ExtensionContext,
'subscriptions' | 'extensionPath'
'subscriptions' | 'extensionUri'
> & {
readonly onResultReceived: (result: OnUiFileUpdatedResult) => void
}

async function onUiFileUpdated({
uri,
extensionPath,
extensionUri,
}: OnUiFileUpdatedArgs): Promise<OnUiFileUpdatedResult> {
const enabled =
workspace
Expand All @@ -43,11 +39,11 @@ async function onUiFileUpdated({

if (!enabled) return { kind: 'Success', value: 'Live execution disabled' }

return compileUi({ extensionPath }, uri)
return compileUi({ extensionUri }, uri)
}

type OnUiFileUpdatedArgs = {
readonly extensionPath: string
readonly extensionUri: URI
readonly uri: URI
}

Expand Down

0 comments on commit c35d822

Please sign in to comment.