Skip to content

Commit

Permalink
Merge pull request #41 from UziTech/tree-view
Browse files Browse the repository at this point in the history
feat: add tree view context menu
  • Loading branch information
aminya authored Dec 7, 2020
2 parents 2eadf1c + 941868f commit e132516
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 52 deletions.
18 changes: 9 additions & 9 deletions menus/terminal.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,41 +32,41 @@
"command": "terminal:close-all"
}
],
"atom-text-editor": [
"atom-text-editor, .tree-view, .tab-bar": [
{
"label": "Terminal",
"submenu": [
{
"label": "Open New Terminal",
"command": "terminal:open"
"command": "terminal:open-context-menu"
},
{
"label": "Open New Terminal (Split Up)",
"command": "terminal:open-up"
"command": "terminal:open-up-context-menu"
},
{
"label": "Open New Terminal (Split Down)",
"command": "terminal:open-down"
"command": "terminal:open-down-context-menu"
},
{
"label": "Open New Terminal (Split Left)",
"command": "terminal:open-left"
"command": "terminal:open-left-context-menu"
},
{
"label": "Open New Terminal (Split Right)",
"command": "terminal:open-right"
"command": "terminal:open-right-context-menu"
},
{
"label": "Open New Terminal (Bottom Dock)",
"command": "terminal:open-bottom-dock"
"command": "terminal:open-bottom-dock-context-menu"
},
{
"label": "Open New Terminal (Left Dock)",
"command": "terminal:open-left-dock"
"command": "terminal:open-left-dock-context-menu"
},
{
"label": "Open New Terminal (Right Dock)",
"command": "terminal:open-right-dock"
"command": "terminal:open-right-dock-context-menu"
},
{
"label": "Close All Terminals",
Expand Down
1 change: 1 addition & 0 deletions spec/model-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ describe("TerminalModel", () => {

it("constructor with previous active item which exists in project path", async () => {
const previousActiveItem = jasmine.createSpyObj("somemodel", ["getPath"])
previousActiveItem.getPath.and.returnValue("/some/dir/file")
spyOn(atom.workspace, "getActivePaneItem").and.returnValue(previousActiveItem)
const expected = ["/some/dir", null]
spyOn(atom.project, "relativizePath").and.returnValue(expected)
Expand Down
27 changes: 27 additions & 0 deletions spec/terminal-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,31 @@ describe("terminal", () => {
expect(newTerminal.runCommand).toHaveBeenCalledWith("command 2")
})
})

describe('open()', () => {
let uri
beforeEach(() => {
uri = terminal.generateNewUri()
spyOn(atom.workspace, 'open')
})

it('simple', async () => {
await terminal.open(uri)

expect(atom.workspace.open).toHaveBeenCalledWith(uri, {})
})

it('target to cwd', async () => {
const testPath = '/test/path'
spyOn(terminal, 'getPath').and.returnValue(testPath)
await terminal.open(
uri,
{ target: true },
)

const url = new URL(atom.workspace.open.calls.mostRecent().args[0])

expect(url.searchParams.get('cwd')).toBe(testPath)
})
})
})
53 changes: 29 additions & 24 deletions src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export class TerminalModel {
this.uri = uri
const url = new URL(this.uri)
this.sessionId = url.host
this.cwd = url.searchParams.get('cwd') || undefined
this.terminalsSet = terminalsSet
this.activeIndex = this.terminalsSet.size
this.title = DEFAULT_TITLE
Expand All @@ -55,41 +56,45 @@ export class TerminalModel {
}

async initialize() {
this.cwd = await this.getInitialCwd()
if (!this.cwd) {
this.cwd = await this.getInitialCwd()
}
}

async getInitialCwd() {
async getInitialCwd(): Promise<string | undefined> {
const previousActiveItem = atom.workspace.getActivePaneItem()
// @ts-ignore
let cwd = previousActiveItem?.getPath?.()
const dir = atom.project.relativizePath(cwd)[0]
if (dir) {
// Use project paths whenever they are available by default.
return dir
if (cwd) {
const dir = atom.project.relativizePath(cwd)[0]
if (dir) {
// Use project paths whenever they are available by default.
return dir
}
}

try {
// Otherwise, if the path exists on the local file system, use the
// path or parent directory as appropriate.
const stats = await fs.stat(cwd)
if (stats.isDirectory()) {
return cwd
}
if (cwd) {
// Otherwise, if the path exists on the local file system, use the
// path or parent directory as appropriate.
const stats = await fs.stat(cwd)
if (stats.isDirectory()) {
return cwd
}

cwd = path.dirname(cwd)
const dirStats = await fs.stat(cwd)
if (dirStats.isDirectory()) {
return cwd
cwd = path.dirname(cwd)
const dirStats = await fs.stat(cwd)
if (dirStats.isDirectory()) {
return cwd
}
}
} catch {}
} catch {
//failt silently
}

cwd = atom.project.getPaths()[0]
// no project paths
if (cwd) {
return cwd
}

return
return cwd
}

serialize() {
Expand Down Expand Up @@ -126,7 +131,7 @@ export class TerminalModel {
return DEFAULT_TITLE + " (" + this.title + ")"
}

onDidChangeTitle(callback: (value?: any) => void) {
onDidChangeTitle(callback: (value?: string) => void) {
return this.emitter.on("did-change-title", callback)
}

Expand All @@ -142,7 +147,7 @@ export class TerminalModel {
return this.modified
}

onDidChangeModified(callback: (value?: any) => void) {
onDidChangeModified(callback: (value?: boolean) => void) {
return this.emitter.on("did-change-modified", callback)
}

Expand Down
Loading

0 comments on commit e132516

Please sign in to comment.