- rename API

- hightlight every expression that has a referenced declaration
pull/1/head
yann300 7 years ago
parent 2f8900f419
commit cd33620fed
  1. 4
      src/app.js
  2. 58
      src/app/editor/contextualListener.js

@ -394,11 +394,11 @@ function run () {
getCompilationResult: () => { getCompilationResult: () => {
return compiler.lastCompilationResult return compiler.lastCompilationResult
}, },
warnFoundCall: (position) => { warnExpression: (position) => {
position = offsetToLineColumnConverter.offsetToLineColumn(position, position.file, compiler.lastCompilationResult) position = offsetToLineColumnConverter.offsetToLineColumn(position, position.file, compiler.lastCompilationResult)
return editor.addMarker(position, config.get('currentFile'), 'highlightcall') return editor.addMarker(position, config.get('currentFile'), 'highlightcall')
}, },
stopFoundCall: (event) => { stopWarningExpression: (event) => {
editor.removeMarker(event.eventId, event.fileTarget) editor.removeMarker(event.eventId, event.fileTarget)
} }
}, { }, {

@ -6,9 +6,8 @@ class ContextualListener {
constructor (api, events) { constructor (api, events) {
this._api = api this._api = api
this._index = { this._index = {
FunctionDefinition: {}, ReferencedDeclarations: {},
FunctionCalls: {}, Expressions: []
FunctionCall: {}
} }
this._events = [] this._events = []
@ -18,27 +17,26 @@ class ContextualListener {
this._buildIndex(data, source) this._buildIndex(data, source)
} else { } else {
this._index = { this._index = {
FunctionDefinition: {}, ReferencedDeclarations: {},
FunctionCalls: {}, Expressions: []
FunctionCall: {}
} }
} }
}) })
this.sourceMappingDecoder = new SourceMappingDecoder() this.sourceMappingDecoder = new SourceMappingDecoder()
this.astWalker = new AstWalker() this.astWalker = new AstWalker()
setInterval(() => { setInterval(() => {
this._context(api.getCursorPosition(), api.getCompilationResult()) this._warnExpressions(api.getCursorPosition(), api.getCompilationResult())
}, 1000) }, 1000)
} }
_context (cursorPosition, compilationResult) { _warnExpressions (cursorPosition, compilationResult) {
if (this.currentPosition === cursorPosition) return if (this.currentPosition === cursorPosition) return
this._stopWarning() this._stopWarning()
this.currentPosition = cursorPosition this.currentPosition = cursorPosition
if (compilationResult && compilationResult.data && compilationResult.source) { if (compilationResult && compilationResult.data && compilationResult.source) {
var nodes = this.sourceMappingDecoder.nodesAtPosition(null, cursorPosition, compilationResult.data.sources[compilationResult.source.target]) var nodes = this.sourceMappingDecoder.nodesAtPosition(null, cursorPosition, compilationResult.data.sources[compilationResult.source.target])
if (nodes && nodes['FunctionCall']) { if (nodes && nodes.length && nodes[nodes.length - 1]) {
this._highlightFunctionCall(nodes['FunctionCall'], compilationResult) this._warnExpression(nodes[nodes.length - 1], compilationResult)
} }
} }
} }
@ -48,13 +46,12 @@ class ContextualListener {
var self = this var self = this
var callback = {} var callback = {}
callback['*'] = function (node) { callback['*'] = function (node) {
if (node && node.name && self._index[node.name]) { if (node && node.attributes && node.attributes.referencedDeclaration) {
self._index[node.name][node.id] = node if (!self._index['ReferencedDeclarations'][node.attributes.referencedDeclaration]) {
if (node.name === 'FunctionCall' && node.children[0] && node.children[0].attributes) { self._index['ReferencedDeclarations'][node.attributes.referencedDeclaration] = []
var declaration = node.children[0].attributes.referencedDeclaration
if (!self._index['FunctionCalls'][declaration]) self._index['FunctionCalls'][declaration] = []
self._index['FunctionCalls'][declaration].push(node.id)
} }
self._index['ReferencedDeclarations'][node.attributes.referencedDeclaration].push(node)
self._index['Expressions'].push(node)
} }
return true return true
} }
@ -62,20 +59,33 @@ class ContextualListener {
} }
} }
_highlightFunctionCall (node, compilationResult) { _warnExpression (node, compilationResult) {
if (node.name === 'FunctionCall' && node.children[0] && node.children[0].attributes) { var self = this
var calls = this._index['FunctionCalls'][node.children[0].attributes.referencedDeclaration] function highlight (id) {
for (var call in calls) { if (self._index['ReferencedDeclarations'] && self._index['ReferencedDeclarations'][id]) {
var position = this.sourceMappingDecoder.decode(this._index['FunctionCall'][calls[call]].src) var calls = self._index['ReferencedDeclarations'][id]
var eventId = this._api.warnFoundCall(position) for (var call in calls) {
this._events.push({ eventId, position, fileTarget: compilationResult.source.target }) self._warn(calls[call].src, compilationResult)
}
} }
} }
if (node.attributes && node.attributes.referencedDeclaration) {
highlight(node.attributes.referencedDeclaration)
} else {
highlight(node.id)
}
}
_warn (src, compilationResult) {
var position = this.sourceMappingDecoder.decode(src)
var eventId = this._api.warnExpression(position)
this._events.push({ eventId, position, fileTarget: compilationResult.source.target })
} }
_stopWarning () { _stopWarning () {
for (var event in this._events) { for (var event in this._events) {
this._api.stopFoundCall(this._events[event]) this._api.stopWarningExpression(this._events[event])
} }
this._events = [] this._events = []
} }

Loading…
Cancel
Save