Skip to content

Commit

Permalink
feat: hit on modules
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyinws committed Apr 27, 2024
1 parent ad35c94 commit dad6999
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 72 deletions.
41 changes: 0 additions & 41 deletions .vscode/settings.json

This file was deleted.

4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
# ext-name

<a href="https://marketplace.visualstudio.com/items?itemName=yuyinws.nuxt-module-intellisense" target="__blank"><img src="https://img.shields.io/visual-studio-marketplace/v/yuyinws.nuxt-module-intellisense.svg?color=eee&amp;label=VS%20Code%20Marketplace&logo=visual-studio-code" alt="Visual Studio Marketplace Version" /></a>

## License

[MIT](./LICENSE) License © 2022 [Anthony Fu](https://github.com/antfu)
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,13 @@
"typecheck": "tsc --noEmit",
"release": "bumpp && nr publish"
},
"dependencies": {
"ast-kit": "^0.12.1"
},
"devDependencies": {
"@antfu/eslint-config": "^2.16.0",
"@antfu/ni": "^0.21.12",
"@babel/types": "^7.24.0",
"@types/node": "^20.12.7",
"@types/vscode": "^1.88.0",
"@vscode/vsce": "^2.26.0",
Expand Down
46 changes: 22 additions & 24 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 53 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,57 @@
import { window } from 'vscode'
import type { CompletionItem, ExtensionContext } from 'vscode'
import { languages, window } from 'vscode'
import type { WithScope } from 'ast-kit'
import { babelParse, walkAST } from 'ast-kit'
import type { Node } from '@babel/types'

export function activate() {
window.showInformationMessage('Hello')
export function activate(context: ExtensionContext) {
const channel = window.createOutputChannel('Nuxt Module Intellisense')
channel.appendLine('Nuxt Module Intellisense')
const provider = languages.registerCompletionItemProvider(
['javascript', 'typescript'],
{
async provideCompletionItems(document, position) {
if (document.fileName.includes('nuxt.config')) {
const fileContent = document.getText()

const ast = babelParse(fileContent, '', {
plugins: ['typescript'],
})

let completionItems: CompletionItem[] = []

walkAST<WithScope<Node>>(ast, {
enter(node) {
if (node.type === 'ObjectProperty' && node.key.loc?.identifierName === 'modules' && node.value.type === 'ArrayExpression') {
const currentModules = node.value.elements

const findIndex = currentModules.findIndex((item) => {
if (item?.loc?.start.line === position.line + 1 && item?.loc?.start.column === position.character - 1)
return true

return false
})

if (findIndex > -1) {
completionItems = [
{ label: 'hello' },
]
}
}
},
})

return completionItems
}
return []
},
},
// 通过引号触发
'\'',
'\"',
)

context.subscriptions.push(provider)
}

export function deactivate() {
Expand Down

0 comments on commit dad6999

Please sign in to comment.