diff --git a/exports/atom.coffee b/exports/atom.coffee index c3601e1cc..81d1726b8 100644 --- a/exports/atom.coffee +++ b/exports/atom.coffee @@ -35,7 +35,6 @@ unless process.env.ATOM_SHELL_INTERNAL_RUN_AS_NODE The `TextEditor` constructor is no longer public. To construct a text editor, use `atom.workspace.buildTextEditor()`. - To check if an object is a text editor, look for for the existence of - a public method that you're using (e.g. `::getText`). + To check if an object is a text editor, use `atom.workspace.isTextEditor(object)`. """ TextEditor diff --git a/spec/workspace-spec.coffee b/spec/workspace-spec.coffee index 5d7343540..7012d2c4b 100644 --- a/spec/workspace-spec.coffee +++ b/spec/workspace-spec.coffee @@ -658,6 +658,13 @@ describe "Workspace", -> waitsForPromise -> workspace.openLicense() runs -> expect(workspace.getActivePaneItem().getText()).toMatch /Copyright/ + describe "::isTextEditor(obj)", -> + it "returns true when the passed object is an instance of `TextEditor`", -> + expect(workspace.isTextEditor(atom.workspace.buildTextEditor())).toBe(true) + expect(workspace.isTextEditor({getText: ->})).toBe(false) + expect(workspace.isTextEditor(null)).toBe(false) + expect(workspace.isTextEditor(undefined)).toBe(false) + describe "::observeTextEditors()", -> it "invokes the observer with current and future text editors", -> observed = [] diff --git a/src/workspace.coffee b/src/workspace.coffee index 396008201..c043c36cf 100644 --- a/src/workspace.coffee +++ b/src/workspace.coffee @@ -518,6 +518,12 @@ class Workspace extends Model @project.bufferForPath(filePath, options).then (buffer) => @buildTextEditor(_.extend({buffer, largeFileMode}, options)) + # Public: Returns a {Boolean} that is `true` if `object` is a `TextEditor`. + # + # * `object` An {Object} you want to perform the check against. + isTextEditor: (object) -> + object instanceof TextEditor + # Extended: Create a new text editor. # # Returns a {TextEditor}.