Skip to content

Commit

Permalink
Fixes #71
Browse files Browse the repository at this point in the history
  • Loading branch information
swr1bm86 committed Apr 9, 2017
1 parent f81162d commit a8a87da
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 9 deletions.
20 changes: 18 additions & 2 deletions src/analysis/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const excludes = [
]

let getImportPattern = () => {
return /import\s+(public\s+)?(([A-Z]\w*)(\.[A-Z]\w*)*)(\s+as\s+\w+)?\r\n/g
return /import\s+(public\s+)?(([A-Z]\w*)(\.[A-Z]\w*)*)(\s+as\s+(\w+))?\r\n/g
}

let getAllIdents = () => {
Expand Down Expand Up @@ -133,6 +133,21 @@ let getImportedModules = (uri) => {
return importedModules
}

let getImportedModuleAndAlias = (uri) => {
let content = fs.readFileSync(uri).toString()
let importPattern = getImportPattern()
let match
let importedModules = []
while (match = importPattern.exec(content)) {
let moduleName = match[2].trim()
if (match[6] != undefined) {
let aliasName = match[6].trim()
importedModules.push({ moduleName, aliasName })
}
}
return importedModules
}

let isDefinitonEqual = (def1, def2) => {
return def1.path == def2.path
&& def1.module == def2.module
Expand All @@ -155,5 +170,6 @@ module.exports = {
getAllIdents,
isDefinitonEqual,
getAllPositions,
getImportPattern
getImportPattern,
getImportedModuleAndAlias
}
30 changes: 23 additions & 7 deletions src/analysis/find-definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,36 @@ let findDefinitionInFile = (definition, uri) => {
return funcDef || adtTypeFuncDef
}

let findDefinitionInFiles = (definition, uri) => {
locations = []
common.getAllFiles('idr').forEach((file) => {
let location = findDefinitionInFile(definition, file)
if (location) {
locations.push(location)
}
let getDefinitionLocations = (identifier) => {
return common.getAllFiles('idr').map((file) => {
return findDefinitionInFile(identifier, file)
}).filter((loc) => {
return loc != null && loc != undefined
})
}

let findDefinitionInFiles = (identifier, uri) => {
let locations = getDefinitionLocations(identifier)
let importedModules = common.getImportedModules(uri)
let legalLocations = locations.filter((loc) => {
return loc.path == uri || importedModules.includes(loc.module)
})
return legalLocations[0]
}

let findDefinitionWithAliasInFiles = (identifier, alias, uri) => {
let locations = getDefinitionLocations(identifier)
let importedModuleAlias = common.getImportedModuleAndAlias(uri)

let legalLocations = locations.filter((loc) => {
let isImported = importedModuleAlias.filter(({ moduleName, aliasName }) => {
return moduleName == loc.module && aliasName == alias
}).length == 1
return loc.path == uri || isImported
})
return legalLocations[0]
}

let findDefinitionForModule = (moduleName) => {
return common.getAllFiles('idr').map((file) => {
let content = fs.readFileSync(file).toString()
Expand All @@ -127,5 +142,6 @@ let findDefinitionForModule = (moduleName) => {

module.exports = {
findDefinitionInFiles,
findDefinitionWithAliasInFiles,
findDefinitionForModule
}
6 changes: 6 additions & 0 deletions src/providers/definitionProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ let IdrisDefinitionProvider = (function () {
if (match && match[2].includes(currentWord)) {
let loc = findDefinition.findDefinitionForModule(match[2])
resolve(loc)
} else if(/(\w+)\.(\w+)/i.test(currentWord)) {
let match = /(\w+)\.(\w+)/i.exec(currentWord)
let moduleAliasName = match[1].trim()
let identifier = match[2].trim()
let loc = findDefinition.findDefinitionWithAliasInFiles(identifier, moduleAliasName, uri)
resolve(loc)
} else {
let loc = findDefinition.findDefinitionInFiles(currentWord, uri)
resolve(loc)
Expand Down

0 comments on commit a8a87da

Please sign in to comment.