Skip to content

Commit

Permalink
feat: support jump to wxml tag definition (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
iChenLei committed Oct 13, 2021
1 parent ceafc4b commit b8e8b22
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
2.3.6 / 2021-10-13
==================

* 支持wxml中tag跳转到自定义的组件

2.3.5 / 2021-10-11
==================

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "minapp-vscode",
"displayName": "WXML - Language Services",
"description": "微信小程序.wxml文件代码高亮,标签、属性的智能补全(同时支持原生小程序、mpvue 和 wepy 框架,并提供code snippets)",
"version": "2.3.5",
"version": "2.3.6",
"publisher": "qiu8310",
"extensionKind": [
"workspace"
Expand Down
3 changes: 2 additions & 1 deletion src/common/src/custom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ async function parseComponentFile(
}
// 实时解析
const attrs = parseAttrs((await readFile(found)).toString())
if (attrs.length) return { attrs } as any
if (attrs.length) return { attrs, path: found } as any
return { path: found } as any;
}
return {} as any
}
Expand Down
22 changes: 22 additions & 0 deletions src/common/src/definition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/******************************************************************
MIT License http://www.opensource.org/licenses/mit-license.php
Author Mora <[email protected]> (https://github.com/qiu8310)
*******************************************************************/

import { Component, CustomOptions, getCustomComponents } from "./custom";
import { LanguageConfig } from "./dev";

export async function definitionTagName(tagName: string, lc: LanguageConfig, co?: CustomOptions): Promise<Component | undefined> {
if (['wxs', 'include'].indexOf(tagName) !== -1 || lc.components.some(item => item.name === tagName)) {
return undefined;
}

const components: Component[] = await getCustomComponents(co);
for (const component of components) {
if (component.name === tagName) {
return component;
}
}

return undefined;
}
1 change: 1 addition & 0 deletions src/common/src/dev/Component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* tslint:disable */
export interface Component {
name: string
path?: string
docLink?: string
since?: string
desc: string[]
Expand Down
1 change: 1 addition & 0 deletions src/common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
export * from './dev/Component'
export * from './autocomplete'
export * from './hover'
export * from './definition'
2 changes: 1 addition & 1 deletion src/common/src/parseAttrs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function parseObjStr(objstr: string) {
return attrs
} catch (e) {
console.log('解析失败:', (e as any)?.message)
console.log(`{${objstr}}`)
// console.log(`{${objstr}}`)
}
return
}
Expand Down
12 changes: 12 additions & 0 deletions src/plugin/PropDefinitionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { DefinitionProvider, TextDocument, Position, CancellationToken, Location
import { getTagAtPosition } from './getTagAtPosition'
import { getClass } from './lib/StyleFile'
import { getProp } from './lib/ScriptFile'
import { definitionTagName } from '../common/src'
import { getCustomOptions, getLanguage } from './lib/helper'

const reserveWords = ['true', 'false']

Expand All @@ -13,6 +15,16 @@ export class PropDefinitionProvider implements DefinitionProvider {
const locs: Location[] = []

if (tag) {
const language = getLanguage(document, position);
if (tag.isOnTagName) {
if (language) {
const component = await definitionTagName(tag.name, language, getCustomOptions(this.config, document));
if (component && component.path) {
locs.push(new Location(Uri.file(component.path), new Position(0, 0)))
}
}
return locs;
}
const { attrs, attrName, posWord } = tag
const rawAttrValue = ((attrs['__' + attrName] || '') as string).replace(/^['"]|['"]$/g, '') // 去除引号

Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"lib": [
"es6"
],
"skipLibCheck": true,
"resolveJsonModule": true,
"sourceMap": true,
"rootDir": "src",
Expand Down

0 comments on commit b8e8b22

Please sign in to comment.