Skip to content

Allow opening files at a specific line and column (fixes #5619) #5620

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

Merged
merged 2 commits into from
Oct 6, 2022
Merged
Show file tree
Hide file tree
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
9 changes: 5 additions & 4 deletions src/node/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { AuthType, DefaultedArgs, Feature, SpawnCodeCli, toCodeArgs, UserProvide
import { coderCloudBind } from "./coder_cloud"
import { commit, version } from "./constants"
import { register } from "./routes"
import { humanPath, isFile, loadAMDModule, open } from "./util"
import { humanPath, isDirectory, loadAMDModule, open } from "./util"

/**
* Return true if the user passed an extension-related VS Code flag.
Expand Down Expand Up @@ -69,14 +69,15 @@ export const openInExistingInstance = async (args: DefaultedArgs, socketPath: st
fileURIs: [],
forceReuseWindow: args["reuse-window"],
forceNewWindow: args["new-window"],
gotoLineMode: true,
}
const paths = args._ || []
for (let i = 0; i < paths.length; i++) {
const fp = path.resolve(paths[i])
if (await isFile(fp)) {
pipeArgs.fileURIs.push(fp)
} else {
if (await isDirectory(fp)) {
pipeArgs.folderURIs.push(fp)
} else {
pipeArgs.fileURIs.push(fp)
}
}
if (pipeArgs.forceNewWindow && pipeArgs.fileURIs.length > 0) {
Expand Down
9 changes: 9 additions & 0 deletions src/node/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,15 @@ export const isFile = async (path: string): Promise<boolean> => {
}
}

export const isDirectory = async (path: string): Promise<boolean> => {
try {
const stat = await fs.stat(path)
return stat.isDirectory()
} catch (error) {
return false
}
}

Comment on lines +485 to +493
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a test for this?

/**
* Escapes any HTML string special characters, like &, <, >, ", and '.
*
Expand Down
27 changes: 25 additions & 2 deletions test/unit/node/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,17 +457,40 @@ describe("isFile", () => {
afterEach(async () => {
await fs.rm(testDir, { recursive: true, force: true })
})
it("should return false if the path doesn't exist", async () => {
it("should return false if is directory", async () => {
expect(await util.isFile(testDir)).toBe(false)
})
it("should return true if is file", async () => {
expect(await util.isFile(pathToFile)).toBe(true)
})
it("should return false if error", async () => {
it("should return false if the path doesn't exist", async () => {
expect(await util.isFile("fakefile.txt")).toBe(false)
})
})

describe("isDirectory", () => {
const testDir = path.join(tmpdir, "tests", "isDirectory")
let pathToFile = ""

beforeEach(async () => {
pathToFile = path.join(testDir, "foo.txt")
await fs.mkdir(testDir, { recursive: true })
await fs.writeFile(pathToFile, "hello")
})
afterEach(async () => {
await fs.rm(testDir, { recursive: true, force: true })
})
it("should return false if is a file", async () => {
expect(await util.isDirectory(pathToFile)).toBe(false)
})
it("should return true if is directory", async () => {
expect(await util.isDirectory(testDir)).toBe(true)
})
it("should return false if the path doesn't exist", async () => {
expect(await util.isDirectory("fakefile.txt")).toBe(false)
})
})

describe("humanPath", () => {
it("should return an empty string if no path provided", () => {
const mockHomedir = "/home/coder"
Expand Down