From b9f0445e591d8d2a8d4c941b175556b8803379e0 Mon Sep 17 00:00:00 2001 From: easyhard Date: Fri, 2 Oct 2015 16:19:38 +0800 Subject: [PATCH] Add TextEditor.getUniqueTitle() This method is intended to replace TextEditor.getLongTitle and gives a better tab title when there are multiple opened files has identical file name. This method will returns /.../ when opened files have identical file name. --- spec/fixtures/testdir/sample-theme-1/readme | 0 .../testdir/sample-theme-1/src/js/main.js | 0 spec/fixtures/testdir/sample-theme-2/readme | 0 .../testdir/sample-theme-2/src/js/main.js | 0 spec/text-editor-spec.coffee | 31 +++++++++++++++ src/text-editor.coffee | 39 +++++++++++++++++++ 6 files changed, 70 insertions(+) create mode 100644 spec/fixtures/testdir/sample-theme-1/readme create mode 100644 spec/fixtures/testdir/sample-theme-1/src/js/main.js create mode 100644 spec/fixtures/testdir/sample-theme-2/readme create mode 100644 spec/fixtures/testdir/sample-theme-2/src/js/main.js 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 e95818900..a3d0ec7f6 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 1e9b6d993..ea844de7b 100644 --- a/src/text-editor.coffee +++ b/src/text-editor.coffee @@ -547,6 +547,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. #