Skip to content

Commit

Permalink
fix: normalize file paths in CompletionContext
Browse files Browse the repository at this point in the history
This commit normalizes file paths in the CompletionContext class to ensure consistent formatting across different operating systems. The `path.normalize` function is used to replace backslashes and forward slashes with the appropriate system separator based on the current platform.

Co-authored-by: Wei Zhang <[email protected]>
  • Loading branch information
Sma1lboy and zwpaper committed Nov 9, 2024
1 parent a7f65b5 commit 5825605
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions clients/tabby-agent/src/codeCompletion/contexts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ export class CompletionContext {
const prefix = this.prefixLines.slice(Math.max(this.prefixLines.length - config.maxPrefixLines, 0)).join("");
const suffix = this.suffixLines.slice(0, config.maxSuffixLines).join("");

// determine system separator
const isWindows = process.platform === "win32";
const separator = isWindows ? "\\" : "/";

// filepath && git_url
let relativeFilepathRoot: string | undefined = undefined;
let filepath: string | undefined = undefined;
Expand All @@ -161,14 +165,16 @@ export class CompletionContext {
relativeFilepathRoot = this.workspace;
}
if (relativeFilepathRoot) {
filepath = path.relative(relativeFilepathRoot, this.filepath);
filepath = path.normalize(path.relative(relativeFilepathRoot, this.filepath)).replace(/[/\\]/g, separator);
}

// declarations
const declarations = this.declarations?.map((declaration) => {
let declarationFilepath = declaration.filepath;
if (relativeFilepathRoot && declarationFilepath.startsWith(relativeFilepathRoot)) {
declarationFilepath = path.relative(relativeFilepathRoot, declarationFilepath);
declarationFilepath = path
.normalize(path.relative(relativeFilepathRoot, declarationFilepath))
.replace(/[/\\]/g, separator);
}
return {
filepath: declarationFilepath,
Expand All @@ -195,7 +201,9 @@ export class CompletionContext {
.map((snippet) => {
let snippetFilepath = snippet.filepath;
if (relativeFilepathRoot && snippetFilepath.startsWith(relativeFilepathRoot)) {
snippetFilepath = path.relative(relativeFilepathRoot, snippetFilepath);
snippetFilepath = path
.normalize(path.relative(relativeFilepathRoot, snippetFilepath))
.replace(/[/\\]/g, separator);
}
return {
filepath: snippetFilepath,
Expand All @@ -209,7 +217,7 @@ export class CompletionContext {
const snippetsOpenedFiles = this.snippetsFromOpenedFiles
?.map((snippet) => {
return {
filepath: snippet.filepath,
filepath: path.normalize(snippet.filepath).replace(/[/\\]/g, separator),
body: snippet.text,
score: snippet.score,
};
Expand Down

0 comments on commit 5825605

Please sign in to comment.