Skip to content
This repository has been archived by the owner on Jun 1, 2023. It is now read-only.

Commit

Permalink
Analyze files with implicit requires (#80)
Browse files Browse the repository at this point in the history
Analyze files with implicit requires

* Add method inside_crystal_path? to avoid analyzing lib and stdlib
* Send clean diagnostics for all files when a full project is analyzed
* Allow analyze files with implicit requires (main file style)
* Adds --no-debug flag for speed up compilation
* Add fake file .scry.cr to analyze a full project (aka main file)
* Remove the fake file .scry.cr from diagnostic list
* Add untitled: to the uri_to_filename method
* Send clean_diagnostic only if  text_document is not inside crystal path
  • Loading branch information
faustinoaq authored Apr 9, 2018
1 parent a8ccc5e commit 07cb031
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 16 deletions.
39 changes: 26 additions & 13 deletions src/scry/analyzer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,40 @@ module Scry
end

def run
@text_document.text.map do |text|
unless inside_path?
if !@text_document.inside_crystal_path?
@text_document.text.map do |text|
analyze(text)
end
end.flatten.uniq
end.flatten.uniq
end
end

private def inside_path?
ENV["CRYSTAL_PATH"].split(':').each do |path|
return true if @text_document.filename.starts_with?(path)
# Reset all diagnostics in the current project
def clean_diagnostic
Dir.glob("#{@workspace.root_uri}/**/*.cr").map do |file|
@diagnostic.clean("file://#{file}")
end
false
end

# NOTE: compiler is a bit heavy in some projects.
# Analyze the code twice,
# first time analyzes a single file and stop if no error
# second time analyzes the entire project requiring the first level files inside src folder
private def analyze(source)
response = crystal_build(@text_document.filename, source)
filename = @text_document.filename
root_uri = @workspace.root_uri
response = crystal_build(filename, source)
if response.empty?
[@diagnostic.clean]
clean_diagnostic
else
@diagnostic.from(response)
if Dir.exists?("#{root_uri}/src") && response.includes?("undefined")
response = crystal_build("#{root_uri}/.scry.cr", %(require "./src/*"))
if response.empty?
clean_diagnostic
else
@diagnostic.from(response)
end
else
@diagnostic.from(response)
end
end
rescue ex
Log.logger.error("A error was found while searching diagnostics\n#{ex}")
Expand All @@ -39,7 +52,7 @@ module Scry
private def crystal_build(filename, source)
code = IO::Memory.new(source)
String.build do |io|
Process.run("crystal", ["build", "--no-codegen", "--no-color", "--error-trace", "-f", "json", "--stdin-filename", filename], output: io, error: io, input: code)
Process.run("crystal", ["build", "--no-debug", "--no-codegen", "--no-color", "--error-trace", "-f", "json", "--stdin-filename", filename], output: io, error: io, input: code)
end
end
end
Expand Down
7 changes: 4 additions & 3 deletions src/scry/publish_diagnostic.cr
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,19 @@ module Scry
notification(params)
end

def clean
params = PublishDiagnosticsParams.new(@uri, [] of Diagnostic)
def clean(uri = @uri)
params = PublishDiagnosticsParams.new(uri, [] of Diagnostic)
notification(params)
end

def from(ex)
def from(ex) : Array(NotificationMessage)
build_failures = Array(BuildFailure).from_json(ex)
build_failures
.uniq
.first(@workspace.max_number_of_problems)
.map { |bf| Diagnostic.new(bf) }
.group_by(&.uri)
.select { |file, diagnostics| !file.ends_with?(".scry.cr") }
.map { |file, diagnostics| unclean(file, diagnostics) }
end
end
Expand Down
7 changes: 7 additions & 0 deletions src/scry/text_document.cr
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ module Scry
uri.starts_with?("untitled:")
end

def inside_crystal_path?
ENV["CRYSTAL_PATH"].split(':').each do |path|
return true if filename.starts_with?(path)
end
false
end

def source
@text.first
end
Expand Down

0 comments on commit 07cb031

Please sign in to comment.