Skip to content

Commit

Permalink
Add more feature to <script> block
Browse files Browse the repository at this point in the history
  • Loading branch information
znck committed Oct 4, 2022
1 parent bc65dfd commit 5724298
Show file tree
Hide file tree
Showing 10 changed files with 776 additions and 258 deletions.
15 changes: 10 additions & 5 deletions packages/compiler-tsx/src/vue/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,16 @@ export function compileWithDecodedSourceMap(
builder.append(
[
`import * as ${resolvedOptions.typeIdentifier} from '${resolvedOptions.typeCheckModuleName}';`,
`import { ${['defineComponent', 'GlobalComponents']
.map(
(id) => `${id} as ${resolvedOptions.internalIdentifierPrefix}${id}`,
)
.join(', ')} } from '${resolvedOptions.runtimeModuleName}';`,
`declare const ${
resolvedOptions.internalIdentifierPrefix
}defineComponent: typeof import(${JSON.stringify(
resolvedOptions.runtimeModuleName,
)}).defineComponent;`,
`type ${
resolvedOptions.internalIdentifierPrefix
}GlobalComponents = import(${JSON.stringify(
resolvedOptions.runtimeModuleName,
)}).GlobalComponents;`,
].join('\n'),
)
builder.nextLine()
Expand Down
124 changes: 49 additions & 75 deletions packages/typescript-plugin-vue/src/features/CodeFixService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { debug } from '@vuedx/shared'
import { VueSFCDocument } from '@vuedx/vue-virtual-textdocument'
import { inject, injectable } from 'inversify'
import type { TSLanguageService, TypeScript } from '../contracts/TypeScript'
import { FilesystemService } from '../services/FilesystemService'
Expand All @@ -16,7 +16,6 @@ export class CodeFixService
private readonly fs: FilesystemService,
) {}

@debug()
public getCodeFixesAtPosition(
fileName: string,
start: number,
Expand All @@ -25,97 +24,72 @@ export class CodeFixService
formatOptions: TypeScript.FormatCodeSettings,
preferences: TypeScript.UserPreferences,
): readonly TypeScript.CodeFixAction[] {
return this.#resolveCodeFixActions(
this.fs.isVueFile(fileName)
? this.#getVueCodeFixesAtPosition(
fileName,
return (
this.pick(fileName, start, {
script: (file) => {
const span = file.findGeneratedTextSpan({
start,
end,
errorCodes,
formatOptions,
preferences,
length: end - start,
})
if (span == null) return []

return this.processCodeFixActions(
this.ts.service.getCodeFixesAtPosition(
file.generatedFileName,
span.start,
span.start + span.length,
errorCodes,
formatOptions,
preferences,
),
)
: this.ts.service.getCodeFixesAtPosition(
fileName,
start,
end,
errorCodes,
formatOptions,
preferences,
),
},
}) ?? []
)
}

#getVueCodeFixesAtPosition(
fileName: string,
start: number,
end: number,
errorCodes: readonly number[],
public getCombinedCodeFix(
scope: TypeScript.CombinedCodeFixScope,
fixId: {},
formatOptions: TypeScript.FormatCodeSettings,
preferences: TypeScript.UserPreferences,
): readonly TypeScript.CodeFixAction[] {
const block = this.fs.getVueFile(fileName)
if (block == null) return []

const genreatedStart = block.generatedOffsetAt(start)
const generatedEnd = block.generatedOffsetAt(end)
if (genreatedStart == null || generatedEnd == null) return []
): TypeScript.CombinedCodeActions {
const file = this.fs.getVueFile(scope.fileName)
if (file == null) return { changes: [], commands: [] }

return this.ts.service.getCodeFixesAtPosition(
block.generatedFileName,
genreatedStart,
generatedEnd,
errorCodes,
const result = this.ts.service.getCombinedCodeFix(
{ ...scope, fileName: file.generatedFileName },
fixId,
formatOptions,
preferences,
)

return {
...result,
changes: this.fs.resolveAllFileTextChanges(result.changes),
}
}

#resolveCodeFixActions(
private pick<R>(
fileName: string,
position: number,
fns: Record<string, (file: VueSFCDocument) => R>,
): R | undefined {
const file = this.fs.getVueFile(fileName)
if (file == null) return
const block = file.getBlockAt(position)
if (block == null) return
const fn = fns[block.type]
if (fn == null) return
return fn(file)
}

public processCodeFixActions(
fixes: readonly TypeScript.CodeFixAction[],
): TypeScript.CodeFixAction[] {
return fixes.map((fix) => ({
...fix,
changes: this.fs.resolveAllFileTextChanges(fix.changes),
}))
}

@debug()
public getCombinedCodeFix(
scope: TypeScript.CombinedCodeFixScope,
fixId: {},
formatOptions: TypeScript.FormatCodeSettings,
preferences: TypeScript.UserPreferences,
): TypeScript.CombinedCodeActions {
const result = this.fs.isVueFile(scope.fileName)
? this.#getVueCombinedCodeFix(scope, fixId, formatOptions, preferences)
: this.ts.service.getCombinedCodeFix(
scope,
fixId,
formatOptions,
preferences,
)

return {
commands: result.commands,
changes: this.fs.resolveAllFileTextChanges(result.changes),
}
}

#getVueCombinedCodeFix(
scope: TypeScript.CombinedCodeFixScope,
fixId: {},
formatOptions: TypeScript.FormatCodeSettings,
preferences: TypeScript.UserPreferences,
): TypeScript.CombinedCodeActions {
const file = this.fs.getVueFile(scope.fileName)
if (file == null) return { changes: [] }

return this.ts.service.getCombinedCodeFix(
{ type: 'file', fileName: file.generatedFileName },
fixId,
formatOptions,
preferences,
)
}
}
Loading

0 comments on commit 5724298

Please sign in to comment.