Skip to content
This repository was archived by the owner on Aug 7, 2023. It is now read-only.

Handling exs #18

Merged
merged 2 commits into from
Oct 20, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
This linter plugin for [Linter][linter] provides an interface to elixirc/mix.
It will be used with files that have the "source.elixir" syntax (ie. `*.ex; *.exs`).

There is a limitation with ElixirC that causes warnings not to be shown when there is a SyntaxError.
There are limitations with ElixirC that:

- in case of compilation error, it will only show first error
- it does not work with buffers, so linting on fly is disabled

## Installation
Plugin requires Linter package and it should install it by itself.
Expand All @@ -27,13 +30,19 @@ Plugin should work with default settings. If not:
2. Packages > Search "linter-elixirc" > Settings
3. Elixirc path - use `which elixirc` to find path. ie. `/usr/local/bin/elixirc`
4. Mix path - use `which mix` to find path. ie. `/usr/local/bin/mix`
5. Always use elixirc - leave it disabled, unless `mix compile` is too slow.

## Usage

If you open folder with mix project (`mix.exs` exists in project's root folder), linter
will use `mix compile` to include all dependencies.
will use `mix compile` to include all dependencies, unless you enable
"Always use elixirc" setting.

If you open single file, linter will use `elixirc`. This will try to find dependencies
in a place where mix projects do (\_build/dev/lib/\*/ebin). If dependency path is different, then
every external dependency will trigger CompileError.

If you open single file, linter will use `elixirc`. This means that every
external dependency will trigger CompileError.
Since `*.ex` files are not compiled by `mix compile`, they are always linted
using `elixirc`.

[linter]: https://github.com/AtomLinter/Linter
100 changes: 56 additions & 44 deletions lib/init.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ module.exports =
type: 'string'
title: 'Mix path'
default: 'mix'
forceElixirc:
type: 'boolean'
title: 'Always use elixirc'
description: 'Activating this will force plugin to never use `mix compile` and always use `elixirc`.'
default: false

activate: ->
require('atom-package-deps').install('linter-elixirc')
Expand All @@ -20,18 +25,25 @@ module.exports =
@subscriptions.add atom.config.observe 'linter-elixirc.mixPath',
(mixPath) =>
@mixPath = mixPath
@subscriptions.add atom.config.observe 'linter-elixirc.forceElixirc',
(forceElixirc) =>
@forceElixirc = forceElixirc
deactivate: ->
@subscriptions.dispose()
provideLinter: ->
helpers = require('atom-linter')
os = require 'os'
fs = require 'fs'
path = require 'path'
project_path = ->
projectPath = ->
atom.project.getPaths()[0]
is_mix_project = ->
fs.existsSync(project_path() + '/mix.exs')
parse_error = (row, textEditor) ->
isMixProject = ->
fs.existsSync(projectPath() + '/mix.exs')
isForcedElixirc = =>
@forceElixirc
isExsFile = (textEditor) ->
textEditor.getPath().endsWith('.exs')
parseError = (row, textEditor) ->
return unless row.startsWith('** ')
re = ///
.*
Expand All @@ -40,65 +52,65 @@ module.exports =
:(\d+): # 3 - line
\ (.*) # 4 - message
///
re_result = re.exec(row)
return unless re_result?
reResult = re.exec(row)
return unless reResult?
ret =
#type: re_result[1]
#type: reResult[1]
type: "Error"
text: re_result[4]
filePath: project_path() + '/' + re_result[2]
range: helpers.rangeFromLineNumber(textEditor, re_result[3] - 1)
parse_warning = (row, textEditor) ->
text: reResult[4]
filePath: projectPath() + '/' + reResult[2]
range: helpers.rangeFromLineNumber(textEditor, reResult[3] - 1)
parseWarning = (row, textEditor) ->
re = ///
([^:]*) # 1 - file name
:(\d+) # 2 - line
:\ warning
:\ (.*) # 3 - message
///
re_result = re.exec(row)
return unless re_result?
reResult = re.exec(row)
return unless reResult?
ret =
type: "Warning"
text: re_result[3]
filePath: project_path() + '/' + re_result[1]
range: helpers.rangeFromLineNumber(textEditor, re_result[2] - 1)
handle_result = (textEditor) ->
(compile_result) ->
result_string = compile_result['stdout'] + "\n" + compile_result['stderr']
result_lines = result_string.split("\n")
error_stack = (parse_error(line, textEditor) for line in result_lines unless !result_lines?)
warning_stack = (parse_warning(line, textEditor) for line in result_lines unless !result_lines?)
(error for error in error_stack.concat(warning_stack) when error?)
text: reResult[3]
filePath: projectPath() + '/' + reResult[1]
range: helpers.rangeFromLineNumber(textEditor, reResult[2] - 1)
handleResult = (textEditor) ->
(compileResult) ->
resultString = compileResult['stdout'] + "\n" + compileResult['stderr']
resultLines = resultString.split("\n")
errorStack = (parseError(line, textEditor) for line in resultLines unless !resultLines?)
warningStack = (parseWarning(line, textEditor) for line in resultLines unless !resultLines?)
(error for error in errorStack.concat(warningStack) when error?)
getFilePathDir = (textEditor) ->
filePath = textEditor.getPath()
path.dirname(filePath)
getOpts = ->
opts =
cwd: project_path()
cwd: projectPath()
throwOnStdErr: false
stream: 'both'
getDepsPa = ->
fs.readdirSync(projectPath() + "/_build/dev/lib/").map (item) ->
"_build/dev/lib/" + item + "/ebin"
lintElixirc = (textEditor) =>
elixircArgs = [
"--ignore-module-conflict", "--app", "mix", "--app", "ex_unit", "-o", os.tmpDir(),
]
elixircArgs.push "-pa", item for item in getDepsPa()
elixircArgs.push textEditor.getPath()
helpers.exec(@elixircPath, elixircArgs, getOpts())
.then(handleResult(textEditor))
lintMix = (textEditor) =>
helpers.exec(@mixPath, ['compile'], getOpts())
.then (handleResult(textEditor))

provider_for_elixirc =
provider =
grammarScopes: ['source.elixir']
scope: 'file'
lintOnFly: false
name: 'Elixir-elixirc'
name: 'Elixir'
lint: (textEditor) =>
elixirc_args = [
"--ignore-module-conflict", "--app", "mix", "--app", "ex_unit", "-o", os.tmpDir(),
getFilePathDir(textEditor)
]
helpers.exec(@elixircPath, elixirc_args, getOpts())
.then(handle_result(textEditor))
provider_for_mix =
grammarScopes: ['source.elixir']
scope: 'file'
lintOnFly: false
name: 'Elixir-Mix'
lint: (textEditor) =>
helpers.exec(@mixPath, ['compile'], getOpts())
.then (handle_result(textEditor))
if is_mix_project()
provider_for_mix
else
provider_for_elixirc
if isForcedElixirc() or not isMixProject() or isExsFile(textEditor)
lintElixirc(textEditor)
else
lintMix(textEditor)