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

Commit

Permalink
Fix formatting untitled file (#65)
Browse files Browse the repository at this point in the history
* Fix formatting unsaved document
* Add spec for untitled files
  • Loading branch information
faustinoaq authored Apr 9, 2018
1 parent 9b1f0fc commit b29a562
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
9 changes: 9 additions & 0 deletions spec/scry/formatter_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,14 @@ module Scry
response = format.run
response.to_json.should eq(FORMATTER_RESPONSE_EXAMPLE)
end

it "check formatter on untitled file" do
workspace = Workspace.new("root_uri", 0, 10)
workspace.put_file(DidOpenTextDocumentParams.from_json(UNTITLED_FORMATTER_EXAMPLE))
text_document, empty_completion = workspace.open_files["Untitled-1"]
format = Formatter.new(workspace, text_document)
response = format.run
response.to_json.should eq(FORMATTER_RESPONSE_EXAMPLE)
end
end
end
2 changes: 2 additions & 0 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@ module Scry
INITIALIZED_EXAMPLE = %({"jsonrpc": "2.0", "method": "initialized", "params": {}})

COMPLETION_EXAMPLE = %({"jsonrpc": "2.0", "method": "textDocument/completion", "params": #{TEXTDOCUMENT_POSITION_PARAM_EXAMPLE}})

UNTITLED_FORMATTER_EXAMPLE = %({"textDocument":{"uri":"untitled:Untitled-1","languageId":"crystal","version":1,"text":"1+1"}})
end
2 changes: 1 addition & 1 deletion src/scry/text_document.cr
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ module Scry
end

def self.uri_to_filename(uri)
uri.sub(/^file:\/\/|^inmemory:\/\/|^git:\/\//, "")
uri.sub(/^file:\/\/|^inmemory:\/\/|^git:\/\/|^untitled:/, "")
end

def in_memory?
Expand Down
10 changes: 7 additions & 3 deletions src/scry/workspace.cr
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ module Scry

def put_file(params : DidOpenTextDocumentParams)
file = TextDocument.new(params)
file_dependencies = @dependency_graph[file.filename].descendants.map &.value
method_db = Completion::MethodDB.generate(file_dependencies)
@open_files[file.filename] = {file, method_db}
if file.untitled?
@open_files[file.filename] = {file, Completion::MethodDB.new}
else
file_dependencies = @dependency_graph[file.filename].descendants.map &.value
method_db = Completion::MethodDB.generate(file_dependencies)
@open_files[file.filename] = {file, method_db}
end
end

def update_file(params : DidChangeTextDocumentParams)
Expand Down

0 comments on commit b29a562

Please sign in to comment.