Merge pull request #9024

This commit is contained in:
Nathan Sobo
2015-11-02 11:26:21 -06:00
6 changed files with 70 additions and 0 deletions

View File

View File

View File

View File

View File

@@ -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 <parent-directory>/<filename> 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 <parent-directory>/.../<filename> 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)

View File

@@ -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,
# * "<filename>" when it is the only editing buffer with this file name.
# * "<unique-dir-prefix>/.../<filename>", 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.
#