Skip to content

Commit

Permalink
feat: improve jump definition (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
iChenLei authored Nov 6, 2021
1 parent 01b3396 commit c68bb63
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = {
extends: ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
rules: {
"no-empty": 0,
"no-prototype-builtins": 0,

"@typescript-eslint/ban-ts-comment": 0,
"@typescript-eslint/no-namespace": 0,
Expand Down
1 change: 0 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { config, configActivate, configDeactivate } from './plugin/lib/config'
import { PropDefinitionProvider } from './plugin/PropDefinitionProvider'

export function activate(context: ExtensionContext): void {
// console.log('minapp-vscode is active!')
configActivate()

if (!config.disableAutoConfig) {
Expand Down
2 changes: 1 addition & 1 deletion src/plugin/WxmlFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type PrettierType = typeof Prettier
export default class implements DocumentFormattingEditProvider, DocumentRangeFormattingEditProvider {
constructor(public config: Config) {}

async format(doc: TextDocument, range: Range, options: FormattingOptions, prefix = '') {
async format(doc: TextDocument, range: Range, options: FormattingOptions, prefix = ''): Promise<TextEdit[]> {
const code = doc.getText(range)
let content: string = code
const resolveOptions = (prettier?: PrettierType) =>
Expand Down
25 changes: 20 additions & 5 deletions src/plugin/lib/ScriptFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ const resultCache = new Map<string, { version: number; data: PropInfo[] }>()
const reservedWords = ['if', 'switch', 'catch', 'while', 'for', 'constructor']

function parseScriptFile(file: string, type: string, prop: string) {
let content = getFileContent(file)
let locs: PropInfo[] = []
const content = getFileContent(file)
const locs: PropInfo[] = []

let reg: RegExp | null = null
/**
Expand Down Expand Up @@ -99,14 +99,29 @@ function parseScriptFile(file: string, type: string, prop: string) {
})
.forEach(mat => {
const property = mat[2] || mat[3] || prop
let pos = getPositionFromIndex(content, mat.index + mat[0].indexOf(property))
let endPos = new Position(pos.line, pos.character + property.length)
const pos = getPositionFromIndex(content, mat.index + mat[0].indexOf(property))
const endPos = new Position(pos.line, pos.character + property.length)
locs.push({
loc: new Location(Uri.file(file), new Range(pos, endPos)),
name: property,
detail: mat[1] || mat[0],
})
})

/**
* 没有匹配到任何有效的定义就直接字符搜索
* 取第一个作为返回
*/
if (locs.length === 0 && content && content.indexOf(prop) !== -1) {
const pos = getPositionFromIndex(content, content.indexOf(prop))
const endPos = new Position(pos.line, pos.character + prop.length)
locs.push({
loc: new Location(Uri.file(file), new Range(pos, endPos)),
name: prop,
detail: prop,
})
}

return locs
}

Expand Down Expand Up @@ -153,7 +168,7 @@ function getVersion(file: string): number {
* @param type
* @param prop
*/
export function getProp(wxmlFile: string, type: string, prop: string) {
export function getProp(wxmlFile: string, type: string, prop: string): PropInfo[] {
const scriptFile = getScriptFile(wxmlFile)
if (!scriptFile) return []

Expand Down

0 comments on commit c68bb63

Please sign in to comment.