Skip to content

Commit

Permalink
normalize uri and CCDB filename on Windows
Browse files Browse the repository at this point in the history
drive letter is now always lower case
  • Loading branch information
rtbo authored and WebFreak001 committed Feb 13, 2024
1 parent 39ebdda commit bf9a55d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
11 changes: 8 additions & 3 deletions protocol/source/served/lsp/uri.d
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ DocumentUri uriFromFile(scope const(char)[] file)
if ((!isAbsolute(file) && !file.startsWith("/"))
|| !file.length)
throw new Exception(text("Tried to pass relative path '", file, "' to uriFromFile"));
file = file.buildNormalizedPath.replace("\\", "/");
file = file.buildNormalizedPath();
version(Windows)
{
file = file.replace("\\", "/");
file = driveName(file).toLower() ~ stripDrive(file);
}
assert(file.length);
if (file.ptr[0] != '/')
file = '/' ~ file; // always triple slash at start but never quad slash
Expand All @@ -34,7 +39,7 @@ unittest

version (Windows)
{

assertEquals(uriFromFile(`C:\Home\foo\bar.d`), `file://c%3A/Home/foo/bar.d`);
}
else
{
Expand Down Expand Up @@ -283,7 +288,7 @@ unittest
{
assertEquals(uriNormalize(`b/../a.d`), `a.d`);
assertEquals(uriNormalize(`b/../../a.d`), `../a.d`);

foreach (prefix; ["file:///", "file://", "", "/", "//"])
{
assertEquals(uriNormalize(prefix ~ `foo/bar/./a.d`), prefix ~ `foo/bar/a.d`);
Expand Down
22 changes: 18 additions & 4 deletions workspace-d/source/workspaced/com/ccdb.d
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module workspaced.com.ccdb;
import std.exception;
import std.json;
import std.path;
import std.string;
import fs = std.file;

import workspaced.api;
Expand Down Expand Up @@ -144,7 +145,7 @@ class ClangCompilationDatabaseComponent : ComponentWrapper
/// in the database.
CompileCommand getCompileCommand(string filename) @property
{
auto normalized = buildNormalizedPath(filename);
const normalized = normalizedCcdbPath(filename);
auto ccp = normalized in _compileCommands;
if (ccp)
return *ccp;
Expand All @@ -171,8 +172,10 @@ public class CompileCommand
import std.algorithm : map;
import std.array : array;

this.directory = enforce("directory" in json, "'directory' missing from Clang compilation database entry")
.str;
this.directory = normalizedCcdbPath(
enforce("directory" in json, "'directory' missing from Clang compilation database entry")
.str
);
this.file = enforce("file" in json, "'file' missing from Clang compilation database entry")
.str;

Expand Down Expand Up @@ -209,7 +212,7 @@ public class CompileCommand

string getNormalizedFilePath() const
{
return getPath(file).buildNormalizedPath();
return normalizedCcdbPath(getPath(file));
}

string getPath(string filename) const
Expand Down Expand Up @@ -303,6 +306,17 @@ public class CompileCommand
}
}

string normalizedCcdbPath(string filePath)
{
const normalized = filePath.buildNormalizedPath();
// Let the drive be lower case on Windows (not handled by buildNormalizedPath).
// This is needed because commands are resolved by simple string comparison.
version (Windows)
return driveName(normalized).toLower() ~ stripDrive(normalized);
else
return normalized;
}

void feedOptions(
in CompileCommand cc,
ref HashSet!string imports,
Expand Down

0 comments on commit bf9a55d

Please sign in to comment.