diff --git a/spec/fixtures/testdir/sample-theme-1/readme b/spec/fixtures/testdir/sample-theme-1/readme new file mode 100644 index 000000000..e69de29bb diff --git a/spec/fixtures/testdir/sample-theme-1/src/js/main.js b/spec/fixtures/testdir/sample-theme-1/src/js/main.js new file mode 100644 index 000000000..e69de29bb diff --git a/spec/fixtures/testdir/sample-theme-2/readme b/spec/fixtures/testdir/sample-theme-2/readme new file mode 100644 index 000000000..e69de29bb diff --git a/spec/fixtures/testdir/sample-theme-2/src/js/main.js b/spec/fixtures/testdir/sample-theme-2/src/js/main.js new file mode 100644 index 000000000..e69de29bb diff --git a/spec/text-editor-spec.coffee b/spec/text-editor-spec.coffee index 8ce026741..8eb0de6e1 100644 --- a/spec/text-editor-spec.coffee +++ b/spec/text-editor-spec.coffee @@ -168,6 +168,37 @@ describe "TextEditor", -> buffer.setPath(undefined) expect(editor.getLongTitle()).toBe 'untitled' + describe ".getUniqueTitle()", -> + it "returns file name when there is no opened file with identical name", -> + expect(editor.getUniqueTitle()).toBe 'sample.js' + buffer.setPath(undefined) + expect(editor.getLongTitle()).toBe 'untitled' + + it "returns / when opened files has identical file names", -> + editor1 = null + editor2 = null + waitsForPromise -> + atom.workspace.open(path.join('sample-theme-1', 'readme')).then (o) -> + editor1 = o + atom.workspace.open(path.join('sample-theme-2', 'readme')).then (o) -> + editor2 = o + runs -> + expect(editor1.getUniqueTitle()).toBe 'sample-theme-1/readme' + expect(editor2.getUniqueTitle()).toBe 'sample-theme-2/readme' + + it "or returns /.../ when opened files has identical file names", -> + editor1 = null + editor2 = null + waitsForPromise -> + atom.workspace.open(path.join('sample-theme-1', 'src', 'js', 'main.js')).then (o) -> + editor1 = o + atom.workspace.open(path.join('sample-theme-2', 'src', 'js', 'main.js')).then (o) -> + editor2 = o + runs -> + expect(editor1.getUniqueTitle()).toBe 'sample-theme-1/.../main.js' + expect(editor2.getUniqueTitle()).toBe 'sample-theme-2/.../main.js' + + it "notifies ::onDidChangeTitle observers when the underlying buffer path changes", -> observed = [] editor.onDidChangeTitle (title) -> observed.push(title) diff --git a/src/text-editor.coffee b/src/text-editor.coffee index 30d5694c4..8a09408dc 100644 --- a/src/text-editor.coffee +++ b/src/text-editor.coffee @@ -573,6 +573,45 @@ class TextEditor extends Model else 'untitled' + # Essential: Get unique title for display in other parts of the UI + # such as the window title. + # + # If the editor's buffer is unsaved, its title is "untitled" + # If the editor's buffer is saved, its unique title is formatted as one + # of the following, + # * "" when it is the only editing buffer with this file name. + # * "/.../", where the "..." may be omitted + # if the the direct parent directory is already different. + # + # Returns a {String} + getUniqueTitle: -> + if sessionPath = @getPath() + title = @getTitle() + + # find text editors with identical file name. + paths = [] + for textEditor in atom.workspace.getTextEditors() when textEditor isnt this + if textEditor.getTitle() is title + paths.push(textEditor.getPath()) + if paths.length is 0 + return title + fileName = path.basename(sessionPath) + + # find the first directory in all these paths that is unique + nLevel = 0 + while (_.some(paths, (apath) -> path.basename(apath) is path.basename(sessionPath))) + sessionPath = path.dirname(sessionPath) + paths = _.map(paths, (apath) -> path.dirname(apath)) + nLevel += 1 + + directory = path.basename sessionPath + if nLevel > 1 + path.join(directory, "...", fileName) + else + path.join(directory, fileName) + else + 'untitled' + # Essential: Get the editor's long title for display in other parts of the UI # such as the window title. #