Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat function autocompletion #42

Merged
merged 6 commits into from
Jul 1, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fix(scriptfile): fix cache validation
  • Loading branch information
NewFuture committed Jun 25, 2019
commit 3b2e952c7919ae2065c42d0ab03683f8c22e43b6
26 changes: 21 additions & 5 deletions src/plugin/lib/ScriptFile.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getFileContent, match, getPositionFromIndex } from './helper'
import * as path from 'path'
import * as fs from 'fs'
import { Location, Uri, Position, Range } from 'vscode'
import { Location, Uri, Position, Range, window } from 'vscode'

interface PropInfo {
loc: Location,
Expand All @@ -15,7 +15,7 @@ const wxJsMapCache = new Map<string, string>()
/**
* 结果缓存
*/
const resultCache = new Map<string, { mtime: number, data: PropInfo[] }>()
const resultCache = new Map<string, { version: number, data: PropInfo[] }>()

/**
* 保留字段,
Expand Down Expand Up @@ -126,6 +126,22 @@ function getScriptFile(wxmlFile: string): string | undefined {
return undefined
}


/**
* 获取文件版本信息,
* 编辑器 和 文件系统
* 只能用===判断
* @param file
*/
function getVersion(file: string): number {
const editor = window.visibleTextEditors.find(e => e.document.fileName === file)
if (editor) {
return editor.document.version
} else {
return fs.statSync(file).mtimeMs
}
}

/**
* 提取脚本文件中的定义
* @param wxmlFile
Expand All @@ -138,13 +154,13 @@ export function getProp(wxmlFile: string, type: string, prop: string) {

const key = `${scriptFile}?${type}&${prop}`
const cache = resultCache.get(key)
const mtime = fs.statSync(scriptFile).mtimeMs
if (cache && cache.mtime === mtime) {
const version = getVersion(scriptFile)
if (cache && cache.version === version) {
return cache.data
}
const result = parseScriptFile(scriptFile, type, prop)
if (result && result.length > 0) {
resultCache.set(key, { mtime, data: result })
resultCache.set(key, { version, data: result })
}
return result
}