diff --git a/types/atom/atom-tests.ts b/types/atom/atom-tests.ts index c846cde9f2..9e765025c8 100644 --- a/types/atom/atom-tests.ts +++ b/types/atom/atom-tests.ts @@ -57,7 +57,15 @@ declare let paneContainer: Atom.Dock|Atom.WorkspaceCenter; // Exports Testing ============================================================ import { BufferedNodeProcess, BufferedProcess, GitRepository, Notification, TextBuffer, TextEditor, Point, Range, File, Directory, Emitter, Disposable, - CompositeDisposable, Task } from "atom"; + CompositeDisposable, Task, watchPath } from "atom"; + +const pathWatcher = watchPath("/var/test", {}, (events) => { + for (const event of events) { + str = event.path; + str = event.action; + if (event.oldPath) str = event.oldPath; + } +}); // global "atom" atom.commands; @@ -86,6 +94,7 @@ atom.inDevMode(); atom.inSafeMode(); atom.inSpecMode(); atom.getVersion(); +str = atom.getReleaseChannel(); atom.isReleasedVersion(); atom.getWindowLoadTime(); @@ -102,8 +111,7 @@ atom.getPosition(); atom.setPosition(42, 42); atom.pickFolder((): void => {}); -const window = atom.getCurrentWindow(); -const [windowWidth, windowHeight] = window.getSize(); +obj = atom.getCurrentWindow(); atom.center(); atom.focus(); @@ -330,6 +338,11 @@ atom.commands.add("test", { "test-function": (event) => {}, "test-function2": (event) => {}, }); +atom.commands.add("test", "test:function", { + didDispatch: (event) => event.stopImmediatePropagation(), + description: "A Command Test", + displayName: "Command: Test", +}); const commands = atom.commands.findCommands({ target: element }); atom.commands.dispatch(element, "test:function"); @@ -906,8 +919,10 @@ pane.moveItem(element, 42); pane.moveItemToPane(element, pane, 42); pane.destroyActiveItem(); -pane.destroyItem(element); -pane.destroyItem(element, true); +async function destroyAndWait() { + bool = await pane.destroyItem(element); + bool = await pane.destroyItem(element, true); +} pane.destroyItems(); pane.destroyInactiveItems(); @@ -970,10 +985,25 @@ bool = panel.isVisible(); panel.hide(); panel.show(); +//// PathWatcher ============================================================== +pathWatcher.dispose(); +sub = pathWatcher.onDidError((error) => str = error.name); + +async function waitForPathWatcher() { + await pathWatcher.getStartPromise(); +} + //// Point -- See 'text-buffer' testing. //// Project ================================================================== // Event Subscription sub = project.onDidChangePaths(paths => paths.length); + +sub = project.onDidChangeFiles(events => { + for (const event of events) { + str = event.action; + } +}); + sub = project.onDidAddBuffer(buffer => buffer.id); sub = project.observeBuffers(buffer => buffer.file); @@ -989,6 +1019,11 @@ async function getDirectoryRepo() { strs = project.getPaths(); project.setPaths(["a", "b"]); project.addPath("Test"); + +async function initWatcher() { + await project.getWatcherPromise("/var/test"); +} + project.removePath("Test"); dirs = project.getDirectories(); diff --git a/types/atom/index.d.ts b/types/atom/index.d.ts index 77e174d138..041084a91e 100644 --- a/types/atom/index.d.ts +++ b/types/atom/index.d.ts @@ -1,11 +1,10 @@ -// Type definitions for Atom 1.20 +// Type definitions for Atom 1.21 // Project: https://github.com/atom/atom // Definitions by: GlenCFL // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.3 /// -/// /// /// /// @@ -113,6 +112,19 @@ declare global { /** Object the new parameters the decoration now has */ newProperties: Structures.DecorationProps; } + + type FilesystemChange = Array<{ + /** A string describing the filesystem action that occurred. */ + action: "created"|"modified"|"deleted"|"renamed"; + + /** The absolute path to the filesystem entry that was acted upon. */ + path: string; + + /** For rename events, a string containing the filesystem entry's former + * absolute path. + */ + oldPath?: string; + }>; } /** Objects that appear as parameters to functions. */ @@ -527,6 +539,11 @@ declare global { /** Get the version of the Atom application. */ getVersion(): string; + /** Gets the release channel of the Atom application. + * Returns the release channel, which can be 'dev', 'beta', or 'stable'. + */ + getReleaseChannel(): "dev"|"beta"|"stable"; + /** Returns a boolean that is true if the current version is an official release. */ isReleasedVersion(): boolean; @@ -564,7 +581,7 @@ declare global { pickFolder(callback: (paths: string[]|null) => void): void; /** Get the current window. */ - getCurrentWindow(): Electron.BrowserWindow; + getCurrentWindow(): object; // Electron's BrowserWindow class. /** Move current window to the center of the screen. */ center(): void; @@ -767,16 +784,24 @@ declare global { * using CSS selectors. */ interface CommandRegistry { - // Register a single command. - add(target: string|Node, commandName: string, callback: (event: - AtomKeymap.CommandEvent) => void): EventKit.Disposable; + /** Register a single command. */ + add(target: string|Node, commandName: string, listener: { + didDispatch(event: AtomKeymap.CommandEvent): void, + displayName?: string, + description?: string, + } | ((event: AtomKeymap.CommandEvent) => void)): EventKit.Disposable; - // Register multiple commands. + /** Register multiple commands. */ add(target: string|Node, commands: { [key: string]: (event: AtomKeymap.CommandEvent) => void }): EventKit.CompositeDisposable; /** Find all registered commands matching a query. */ - findCommands(params: { target: Node }): Array<{ name: string, displayName: string }>; + findCommands(params: { target: Node }): Array<{ + name: string, + displayName: string, + description?: string, + tags?: string[], + }>; /** Simulate the dispatch of a command on a DOM node. */ dispatch(target: Node, commandName: string): void; @@ -1073,7 +1098,7 @@ declare global { /** Hide the dock and activate the WorkspaceCenter if the dock was was previously focused. */ hide(): void; - /** Toggle the dock's visiblity without changing the Workspace's active pane container. */ + /** Toggle the dock's visibility without changing the Workspace's active pane container. */ toggle(): void; /** Check if the dock is visible. */ @@ -1672,7 +1697,7 @@ declare global { destroyActiveItem(): void; /** Destroy the given item. */ - destroyItem(item: object, force?: boolean): void; + destroyItem(item: object, force?: boolean): Promise; /** Destroy all items. */ destroyItems(): void; @@ -1779,6 +1804,23 @@ declare global { show(): void; } + /** Manage a subscription to filesystem events that occur beneath a root directory. */ + interface PathWatcher extends EventKit.DisposableLike { + /** Return a Promise that will resolve when the underlying native watcher is + * ready to begin sending events. + */ + getStartPromise(): Promise; + + /** Invokes a function when any errors related to this watcher are reported. */ + onDidError(callback: (error: Error) => void): EventKit.Disposable; + + /** Unsubscribe all subscribers from filesystem events. Native resources will be + * release asynchronously, but this watcher will stop broadcasting events + * immediately. + */ + dispose(): void; + } + /** Represents a project that's opened in Atom. */ interface Project { // Event Subscription @@ -1793,6 +1835,9 @@ declare global { */ observeBuffers(callback: (buffer: TextBuffer.TextBuffer) => void): EventKit.Disposable; + /** Invoke a callback when a filesystem change occurs within any open project path. */ + onDidChangeFiles(callback: (events: Events.FilesystemChange) => void): EventKit.Disposable; + // Accessing the Git Repository /** Get an Array of GitRepositorys associated with the project's directories. */ getRepositories(): GitRepository[]; @@ -1810,6 +1855,11 @@ declare global { /** Add a path to the project's list of root paths. */ addPath(projectPath: string): void; + /** Access a promise that resolves when the filesystem watcher associated with a + * project root directory is ready to begin receiving events. + */ + getWatcherPromise(projectPath: string): Promise; + /** Remove a path from the project's list of root paths. */ removePath(projectPath: string): void; @@ -2863,36 +2913,36 @@ declare global { { reversed?: boolean, preserveFolds?: boolean }): Selection; /** Select from the current cursor position to the given position in buffer coordinates. - * This method may merge selections that end up intesecting. + * This method may merge selections that end up intersecting. */ selectToBufferPosition(position: TextBuffer.PointLike|[number, number]): void; /** Select from the current cursor position to the given position in screen coordinates. - * This method may merge selections that end up intesecting. + * This method may merge selections that end up intersecting. */ selectToScreenPosition(position: TextBuffer.PointLike|[number, number]): void; /** Move the cursor of each selection one character upward while preserving the * selection's tail position. - * This method may merge selections that end up intesecting. + * This method may merge selections that end up intersecting. */ selectUp(rowCount?: number): void; /** Move the cursor of each selection one character downward while preserving * the selection's tail position. - * This method may merge selections that end up intesecting. + * This method may merge selections that end up intersecting. */ selectDown(rowCount?: number): void; /** Move the cursor of each selection one character leftward while preserving * the selection's tail position. - * This method may merge selections that end up intesecting. + * This method may merge selections that end up intersecting. */ selectLeft(columnCount?: number): void; /** Move the cursor of each selection one character rightward while preserving * the selection's tail position. - * This method may merge selections that end up intesecting. + * This method may merge selections that end up intersecting. */ selectRight(columnCount?: number): void; @@ -2913,7 +2963,7 @@ declare global { /** Move the cursor of each selection to the beginning of its line while preserving * the selection's tail position. - * This method may merge selections that end up intesecting. + * This method may merge selections that end up intersecting. */ selectToBeginningOfLine(): void; @@ -3650,6 +3700,7 @@ declare global { item: object, visible?: boolean, priority?: number, + autoFocus?: boolean, }): Panel; /** Returns the Panel associated with the given item or null when the item @@ -3811,6 +3862,7 @@ declare global { type PaneItemMoved = AtomCore.Events.PaneItemMoved; type CursorPositionChanged = AtomCore.Events.CursorPositionChanged; type DecorationPropsChanged = AtomCore.Events.DecorationPropsChanged; + type FilesystemChange = AtomCore.Events.FilesystemChange; } /** Objects that appear as parameters to functions. */ @@ -3910,8 +3962,6 @@ declare global { /** Represents an individual file that can be watched, read from, and written to. */ type File = PathWatcher.File; - type PathWatcher = PathWatcher.PathWatcher; - // Text Buffer ============================================================== /** The interface that should be implemented for all "point-compatible" objects. */ /** Represents a buffer annotation that remains logically stationary even as the @@ -4055,6 +4105,9 @@ declare global { */ type Panel = AtomCore.Panel; + /** Manage a subscription to filesystem events that occur beneath a root directory. */ + type PathWatcher = AtomCore.PathWatcher; + /** Represents a project that's opened in Atom. */ type Project = AtomCore.Project; @@ -4145,3 +4198,10 @@ export const TextBuffer: TextBuffer.Statics.TextBuffer; * including cursor and selection positions, folds, and soft wraps. */ export const TextEditor: AtomCore.Statics.TextEditor; + +/** Invoke a callback with each filesystem event that occurs beneath a specified path. + * If you only need to watch events within the project's root paths, use + * Project::onDidChangeFiles instead. + */ +export function watchPath(rootPath: string, options: {}, eventCallback: (events: + AtomCore.Events.FilesystemChange) => void): AtomCore.PathWatcher; diff --git a/types/atom/package.json b/types/atom/package.json deleted file mode 100644 index fe9d276dc5..0000000000 --- a/types/atom/package.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "private": true, - "dependencies": { - "electron": "1.6.9" - } -} diff --git a/types/pathwatcher/index.d.ts b/types/pathwatcher/index.d.ts index b3d99b81ef..cf65fcc2d0 100644 --- a/types/pathwatcher/index.d.ts +++ b/types/pathwatcher/index.d.ts @@ -7,6 +7,8 @@ /// /// +import { ReadStream, WriteStream } from "fs"; + declare global { namespace PathWatcher { /** Objects that appear as parameters to callbacks. */ @@ -128,13 +130,13 @@ declare global { read(flushCache?: boolean): Promise; /** Returns a stream to read the content of the file. */ - createReadStream(): NodeJS.ReadableStream; + createReadStream(): ReadStream; /** Overwrites the file with the given text. */ write(text: string): Promise; /** Returns a stream to write content to the file. */ - createWriteStream(): NodeJS.WritableStream; + createWriteStream(): WriteStream; /** Overwrites the file with the given text. */ writeSync(text: string): undefined; diff --git a/types/pathwatcher/pathwatcher-tests.ts b/types/pathwatcher/pathwatcher-tests.ts index b4144ebd3d..60391ed480 100644 --- a/types/pathwatcher/pathwatcher-tests.ts +++ b/types/pathwatcher/pathwatcher-tests.ts @@ -59,7 +59,8 @@ async function readFile() { str = await file.read(); } -file.createReadStream(); +const stream = file.createReadStream(); +stream.close(); async function writeFile() { await file.write("Test"); diff --git a/types/text-buffer/index.d.ts b/types/text-buffer/index.d.ts index 663d62914c..feae93f5b1 100644 --- a/types/text-buffer/index.d.ts +++ b/types/text-buffer/index.d.ts @@ -844,8 +844,11 @@ declare global { onDidChangeEncoding(callback: (encoding: string) => void): EventKit.Disposable; - /** Invoke the given callback before the buffer is saved to disk. */ - onWillSave(callback: () => void): EventKit.Disposable; + /** Invoke the given callback before the buffer is saved to disk. If the + * given callback returns a promise, then the buffer will not be saved until + * the promise resolves. + */ + onWillSave(callback: () => Promise|void): EventKit.Disposable; /** Invoke the given callback after the buffer is saved to disk. */ onDidSave(callback: (event: Events.FileSaved) => void): diff --git a/types/text-buffer/text-buffer-tests.ts b/types/text-buffer/text-buffer-tests.ts index 4594ac4c41..52e4383158 100644 --- a/types/text-buffer/text-buffer-tests.ts +++ b/types/text-buffer/text-buffer-tests.ts @@ -199,7 +199,10 @@ sub = buffer.onDidChangePath((path): void => { }); sub = buffer.onDidChangeEncoding(() => void {}); + sub = buffer.onWillSave(() => void {}); +sub = buffer.onWillSave(() => Promise.resolve()); + sub = buffer.onDidSave(() => void {}); sub = buffer.onDidDelete(() => void {}); sub = buffer.onWillReload(() => void {});