Skip to content

Commit b562d4a

Browse files
authored
Allow opening files at a specific line and column (fixes #5619) (#5620)
* Allow opening files at a specific line and column (fixes #5619) * Add isDirectory test
1 parent 3a9eb31 commit b562d4a

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

src/node/main.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { AuthType, DefaultedArgs, Feature, SpawnCodeCli, toCodeArgs, UserProvide
99
import { coderCloudBind } from "./coder_cloud"
1010
import { commit, version } from "./constants"
1111
import { register } from "./routes"
12-
import { humanPath, isFile, loadAMDModule, open } from "./util"
12+
import { humanPath, isDirectory, loadAMDModule, open } from "./util"
1313

1414
/**
1515
* Return true if the user passed an extension-related VS Code flag.
@@ -69,14 +69,15 @@ export const openInExistingInstance = async (args: DefaultedArgs, socketPath: st
6969
fileURIs: [],
7070
forceReuseWindow: args["reuse-window"],
7171
forceNewWindow: args["new-window"],
72+
gotoLineMode: true,
7273
}
7374
const paths = args._ || []
7475
for (let i = 0; i < paths.length; i++) {
7576
const fp = path.resolve(paths[i])
76-
if (await isFile(fp)) {
77-
pipeArgs.fileURIs.push(fp)
78-
} else {
77+
if (await isDirectory(fp)) {
7978
pipeArgs.folderURIs.push(fp)
79+
} else {
80+
pipeArgs.fileURIs.push(fp)
8081
}
8182
}
8283
if (pipeArgs.forceNewWindow && pipeArgs.fileURIs.length > 0) {

src/node/util.ts

+9
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,15 @@ export const isFile = async (path: string): Promise<boolean> => {
482482
}
483483
}
484484

485+
export const isDirectory = async (path: string): Promise<boolean> => {
486+
try {
487+
const stat = await fs.stat(path)
488+
return stat.isDirectory()
489+
} catch (error) {
490+
return false
491+
}
492+
}
493+
485494
/**
486495
* Escapes any HTML string special characters, like &, <, >, ", and '.
487496
*

test/unit/node/util.test.ts

+25-2
Original file line numberDiff line numberDiff line change
@@ -457,17 +457,40 @@ describe("isFile", () => {
457457
afterEach(async () => {
458458
await fs.rm(testDir, { recursive: true, force: true })
459459
})
460-
it("should return false if the path doesn't exist", async () => {
460+
it("should return false if is directory", async () => {
461461
expect(await util.isFile(testDir)).toBe(false)
462462
})
463463
it("should return true if is file", async () => {
464464
expect(await util.isFile(pathToFile)).toBe(true)
465465
})
466-
it("should return false if error", async () => {
466+
it("should return false if the path doesn't exist", async () => {
467467
expect(await util.isFile("fakefile.txt")).toBe(false)
468468
})
469469
})
470470

471+
describe("isDirectory", () => {
472+
const testDir = path.join(tmpdir, "tests", "isDirectory")
473+
let pathToFile = ""
474+
475+
beforeEach(async () => {
476+
pathToFile = path.join(testDir, "foo.txt")
477+
await fs.mkdir(testDir, { recursive: true })
478+
await fs.writeFile(pathToFile, "hello")
479+
})
480+
afterEach(async () => {
481+
await fs.rm(testDir, { recursive: true, force: true })
482+
})
483+
it("should return false if is a file", async () => {
484+
expect(await util.isDirectory(pathToFile)).toBe(false)
485+
})
486+
it("should return true if is directory", async () => {
487+
expect(await util.isDirectory(testDir)).toBe(true)
488+
})
489+
it("should return false if the path doesn't exist", async () => {
490+
expect(await util.isDirectory("fakefile.txt")).toBe(false)
491+
})
492+
})
493+
471494
describe("humanPath", () => {
472495
it("should return an empty string if no path provided", () => {
473496
const mockHomedir = "/home/coder"

0 commit comments

Comments
 (0)