Skip to content

Commit

Permalink
refactor: template completions (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
znck authored Dec 17, 2020
1 parent 38de79c commit e1e26cb
Show file tree
Hide file tree
Showing 51 changed files with 2,140 additions and 949 deletions.
6 changes: 5 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@
"standard-with-typescript",
"prettier",
"prettier/@typescript-eslint"
]
],
"rules": {
"no-void": "off",
"@typescript-eslint/method-signature-style": [2, "method"]
}
},
{
"files": ["**/__tests__/**/*.ts", "**/*.spec.ts"],
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/insiders.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ jobs:

- name: Publish Insiders Packages
run: |
echo '//registry.npmjs.org/:_authToken=${NPM_TOKEN}' >> .npmrc
pnpm recursive --filter ./packages exec -- npm version prerelease --preid=insiders-$(date +%s)
pnpm recursive --filter ./packages publish --tag insiders --access public --no-git-checks
env:
Expand Down
1 change: 0 additions & 1 deletion .npmrc
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
scope=@vuedx
//registry.npmjs.org/:_authToken=${NPM_TOKEN}
19 changes: 18 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Attach to TSServer",
"port": 9999,
"request": "attach",
"skipFiles": ["<node_internals>/**"],
"type": "pwa-node",
"outFiles": ["${workspaceFolder}/packages/*/dist/*.js"]
},
{
"type": "node",
"name": "Run tests in current file",
Expand All @@ -18,11 +26,14 @@
"type": "extensionHost",
"request": "launch",
"runtimeExecutable": "${execPath}",
"env": {
"TSS_DEBUG": "9999"
},
"args": [
"--disable-extensions",
"--extensionDevelopmentPath=${workspaceRoot}/extensions/vscode-vue",
"--extensionDevelopmentPath=${workspaceRoot}/extensions/vscode-vue-language-features",
"${workspaceRoot}/samples/feature-refactor"
"${workspaceRoot}/samples/feature-diagnostics"
],

"outFiles": [
Expand All @@ -45,5 +56,11 @@
"${workspaceRoot}/extensions/vscode-vue-language-features/dist/*.js"
]
}
],
"compounds": [
{
"name": "Debug: Extension: VS Code",
"configurations": ["Extension: VS Code", "Attach to TSServer"]
}
]
}
Binary file added assets/preview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const workingDir = Path.dirname(__dirname)

const files = [
'node_modules/@vuedx/typescript-standalone/dist/index.js',
'node_modules/@vuedx/typescript-standalone/dist/index.js.map',
'node_modules/@vuedx/typescript-standalone/package.json',
]
execSync(`mkdir -p tmp`, { cwd: workingDir })
Expand Down
Binary file modified extensions/vscode-vue-language-features/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 7 additions & 5 deletions extensions/vscode-vue-language-features/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"preview": false,
"name": "vue-language-features",
"publisher": "znck",
"displayName": "Vue Language Features",
"displayName": "VueDX",
"version": "2020.12.10",
"description": "Enhanced TypeScript/JavaScript Support",
"description": "Advanced TypeScript/JavaScript support for Vue",
"icon": "logo.png",
"main": "dist/index.js",
"activationEvents": [
Expand Down Expand Up @@ -73,7 +73,7 @@
},
"vue.implementationsCodeLens.enabled": {
"type": "boolean",
"default": false,
"default": true,
"description": "Enable/disable implementations CodeLens. This CodeLens shows the implementers of an interface.",
"scope": "window"
},
Expand All @@ -85,7 +85,7 @@
},
"vue.referencesCodeLens.enabled": {
"type": "boolean",
"default": false,
"default": true,
"description": "Enable/disable references CodeLens in TypeScript/JavaScript files.",
"scope": "window"
},
Expand Down Expand Up @@ -132,7 +132,7 @@
],
"typescriptServerPlugins": [
{
"name": "@vuedx/typescript-standalone",
"name": "@vuedx/typescript-plugin-vue",
"enableForWorkspaceTypeScriptVersions": true,
"configNamespace": "vue",
"languages": [
Expand All @@ -152,8 +152,10 @@
"json5": "^2.1.3"
},
"devDependencies": {
"@types/vscode": "^1.52.0",
"@vercel/ncc": "^0.24.1",
"@vuedx/vue-virtual-textdocument": "workspace:*",
"@vuedx/typescript-plugin-vue": "workspace:*",
"inversify": "^5.0.1",
"reflect-metadata": "^0.1.13",
"typescript": "^4.0.3",
Expand Down
11 changes: 8 additions & 3 deletions extensions/vscode-vue-language-features/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

This extension provides features like type checking, completion, renaming and refactoring for `.vue` files by extending TypeScript extension.

<!-- INSIDERS -->

## Support

This extension is part of [VueDX project](https://github.com/znck/vue-developer-experience), maintained by [Rahul Kadyan](https://github.com/znck). You can [💖 sponsor him](https://github.com/sponsors/znck) for continued development of this extension and other VueDX tools.

<!-- CHANGELOG -->

## Features

### Module Resolution
Expand Down Expand Up @@ -165,10 +169,11 @@ This extension is part of [VueDX project](https://github.com/znck/vue-developer-

## Known Issues

- Required Vue 3.0+
- Works better if `tsconfig.json` or `jsconfig.json`
- Requires Vue 3.0+
- Works better if project has `tsconfig.json` or `jsconfig.json`
- Diagnostics messages complain about JSX/TSX
- Prop rename does work in all cases
- Renaming props does not work in all cases
- Does not work in newly created files

---

Expand Down
4 changes: 4 additions & 0 deletions extensions/vscode-vue-language-features/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { OpenVirtualFileCommand } from './commands/openVirtualFile'
import { VueVirtualDocumentProvider } from './scheme/vue'
import { DocumentService } from './services/documents'
import { VirtualFileSwitcher } from './services/VirtualFileSwitcher'
import { StyleLanguageProxy } from './services/StyleLanguageProxy'
import { TemplateLanguageProxy } from './services/TemplateLanguageProxy'

export async function activate(
context: vscode.ExtensionContext,
Expand All @@ -18,6 +20,8 @@ export async function activate(
container.bind('context').toConstantValue(context)
context.subscriptions.push(
container.get(DocumentService).install(),
container.get(StyleLanguageProxy).install(),
container.get(TemplateLanguageProxy).install(),
container.get(VueVirtualDocumentProvider).install(),
container.get(OpenVirtualFileCommand).install(),
container.get(VirtualFileSwitcher).install(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { VirtualTextDocument } from '@vuedx/vue-virtual-textdocument'
import { injectable } from 'inversify'
import {
CancellationToken,
commands,
CompletionContext,
CompletionItem,
CompletionItemProvider,
CompletionList,
Disposable,
languages,
Position,
Range,
TextDocument,
Uri,
} from 'vscode'
import { Installable } from '../utils/installable'
import { DocumentService } from './documents'

@injectable()
export class StyleLanguageProxy
extends Installable
implements CompletionItemProvider {
constructor(private readonly documents: DocumentService) {
super()
}

private readonly selector = { language: 'vue' }
private readonly triggerCharacters = ['.']

public install(): Disposable {
return Disposable.from(
languages.registerCompletionItemProvider(this.selector, this),
)
}

async provideCompletionItems(
container: TextDocument,
position: Position,
_: CancellationToken,
context: CompletionContext,
): Promise<
null | undefined | CompletionItem[] | CompletionList<CompletionItem>
> {
const document = await this.getDocumentAt(container, position)

if (this.isSupportDocumentType(document)) {
const result = await commands.executeCommand<CompletionItem[]>(
'vscode.executeCompletionItemProvider',
this.getUri(document),
position,
context.triggerCharacter,
)

return result
}

return null
}

private readonly languages = new Set([
'css',
'scss',
'sass',
'less',
'stylus',
])

private isSupportDocumentType(
document: VirtualTextDocument | undefined,
): document is VirtualTextDocument {
if (document == null) return false

return this.languages.has(document.languageId)
}

private async getDocumentAt(
container: TextDocument,
position: Position,
): Promise<VirtualTextDocument | undefined> {
const document = await this.documents.asVueDocument(container)

return document.documentAt(position)
}

private async getStyleDocuments(
container: TextDocument,
range?: Range,
): Promise<VirtualTextDocument[]> {
const document = await this.documents.asVueDocument(container)
let styles = document.descriptor.styles
if (range != null) {
styles = styles.filter(({ loc: { start, end } }) =>
range.contains(
new Range(start.line, start.column, end.line, end.column),
),
)
}

return styles.map((style) =>
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
document.getDocument(document.getBlockSelector(style)!),
)
}

private getUri(document: VirtualTextDocument): Uri {
return Uri.parse(document.uri)
}
}
Loading

0 comments on commit e1e26cb

Please sign in to comment.