Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve handling of paths as URIs #66

Closed
wants to merge 2 commits into from
Closed
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
36 changes: 29 additions & 7 deletions src/nimlsp.nim
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ const
# This is used to explicitly set the default source path
explicitSourcePath {.strdefine.} = getCurrentCompilerExe().parentDir.parentDir

type
UriParseError* = object of Defect
uri: string

var nimpath = explicitSourcePath

discard existsOrCreateDir(storage)
Expand Down Expand Up @@ -105,8 +109,22 @@ proc pathToUri(path: string): string =

proc uriToPath(uri: string): string =
## Convert an RFC 8089 file URI to a native, platform-specific, absolute path.
let startIdx = when defined(windows): 8 else: 7
normalizedPath(uri[startIdx..^1])
#let startIdx = when defined(windows): 8 else: 7
#normalizedPath(uri[startIdx..^1])
let parsed = uri.parseUri
if parsed.scheme != "file":
var e = newException(UriParseError, "Invalid scheme: " & parsed.scheme & ", only \"file\" is supported")
e.uri = uri
raise e
if parsed.hostname != "":
var e = newException(UriParseError, "Invalid hostname: " & parsed.hostname & ", only empty hostname is supported")
e.uri = uri
raise e
return normalizedPath(
when defined(windows):
parsed.path[1..^1]
else:
parsed.path).decodeUrl

proc parseId(node: JsonNode): int =
if node.kind == JString:
Expand Down Expand Up @@ -324,7 +342,7 @@ while true:
for suggestion in suggestions:
if suggestion.section == ideUse or referenceRequest["context"]["includeDeclaration"].getBool:
response.add create(Location,
"file://" & suggestion.filepath,
"file://" & pathToUri(suggestion.filepath),
create(Range,
create(Position, suggestion.line-1, suggestion.column),
create(Position, suggestion.line-1, suggestion.column + suggestion.qualifiedPath[^1].len)
Expand All @@ -351,9 +369,9 @@ while true:
else:
var textEdits = newJObject()
for suggestion in suggestions:
if not textEdits.hasKey("file://" & suggestion.filepath):
textEdits["file://" & suggestion.filepath] = newJArray()
textEdits["file://" & suggestion.filepath].add create(TextEdit,
if not textEdits.hasKey("file://" & pathToUri(suggestion.filepath)):
textEdits["file://" & pathToUri(suggestion.filepath)] = newJArray()
textEdits["file://" & pathToUri(suggestion.filepath)].add create(TextEdit,
create(Range,
create(Position, suggestion.line-1, suggestion.column),
create(Position, suggestion.line-1, suggestion.column + suggestion.qualifiedPath[^1].len)
Expand Down Expand Up @@ -540,7 +558,11 @@ while true:
else:
debugEcho "Got unknown notification message"
continue
except IOError:
except UriParseError as e:
debugEcho "Got exception parsing URI: ", e.msg
continue
except IOError as e:
debugEcho "Got IOError: ", e.msg
break
except CatchableError as e:
debugEcho "Got exception: ", e.msg
Expand Down