From b8e8b22f1c571a769c841683d863b8180115b5ce Mon Sep 17 00:00:00 2001 From: Lei Chen Date: Wed, 13 Oct 2021 15:20:22 +0800 Subject: [PATCH] feat: support jump to wxml tag definition (#115) --- CHANGELOG.md | 5 +++++ package-lock.json | 2 +- package.json | 2 +- src/common/src/custom.ts | 3 ++- src/common/src/definition.ts | 22 ++++++++++++++++++++++ src/common/src/dev/Component.ts | 1 + src/common/src/index.ts | 1 + src/common/src/parseAttrs.ts | 2 +- src/plugin/PropDefinitionProvider.ts | 12 ++++++++++++ tsconfig.json | 1 + 10 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 src/common/src/definition.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 098682b..edd1a26 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +2.3.6 / 2021-10-13 +================== + +* 支持wxml中tag跳转到自定义的组件 + 2.3.5 / 2021-10-11 ================== diff --git a/package-lock.json b/package-lock.json index e2d3e86..d56dbe1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "minapp-vscode", - "version": "2.3.5", + "version": "2.3.6", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index c388109..9f6bed5 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/src/common/src/custom.ts b/src/common/src/custom.ts index 464f204..4e1015d 100644 --- a/src/common/src/custom.ts +++ b/src/common/src/custom.ts @@ -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 } diff --git a/src/common/src/definition.ts b/src/common/src/definition.ts new file mode 100644 index 0000000..3b5f6bc --- /dev/null +++ b/src/common/src/definition.ts @@ -0,0 +1,22 @@ +/****************************************************************** + MIT License http://www.opensource.org/licenses/mit-license.php + Author Mora (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 { + 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; +} \ No newline at end of file diff --git a/src/common/src/dev/Component.ts b/src/common/src/dev/Component.ts index 73c9a95..fb1971e 100644 --- a/src/common/src/dev/Component.ts +++ b/src/common/src/dev/Component.ts @@ -1,6 +1,7 @@ /* tslint:disable */ export interface Component { name: string + path?: string docLink?: string since?: string desc: string[] diff --git a/src/common/src/index.ts b/src/common/src/index.ts index 5211ebd..b564581 100644 --- a/src/common/src/index.ts +++ b/src/common/src/index.ts @@ -6,3 +6,4 @@ export * from './dev/Component' export * from './autocomplete' export * from './hover' +export * from './definition' diff --git a/src/common/src/parseAttrs.ts b/src/common/src/parseAttrs.ts index ce4d37b..f91c128 100644 --- a/src/common/src/parseAttrs.ts +++ b/src/common/src/parseAttrs.ts @@ -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 } diff --git a/src/plugin/PropDefinitionProvider.ts b/src/plugin/PropDefinitionProvider.ts index e8e5c0a..34d553f 100644 --- a/src/plugin/PropDefinitionProvider.ts +++ b/src/plugin/PropDefinitionProvider.ts @@ -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'] @@ -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, '') // 去除引号 diff --git a/tsconfig.json b/tsconfig.json index 797a85a..d3f6045 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,6 +6,7 @@ "lib": [ "es6" ], + "skipLibCheck": true, "resolveJsonModule": true, "sourceMap": true, "rootDir": "src",