diff --git a/lib/decoration-management.js b/lib/decoration-management.js index 469dd721..107bb7c6 100644 --- a/lib/decoration-management.js +++ b/lib/decoration-management.js @@ -80,7 +80,7 @@ export default class DecorationManagement { * * @return {Array} all the decorations in this `Minimap` */ - getDecorations() { + getDecorations(): Array { return [...this.decorationsById.values()] } @@ -96,7 +96,7 @@ export default class DecorationManagement { * - decoration: the decoration object that was created * @return {Disposable} a disposable to stop listening to the event */ - onDidAddDecoration(callback) { + onDidAddDecoration(callback: (event: Object) => void): Disposable { return this.emitter.on("did-add-decoration", callback) } @@ -112,7 +112,7 @@ export default class DecorationManagement { * - decoration: the decoration object that was created * @return {Disposable} a disposable to stop listening to the event */ - onDidRemoveDecoration(callback) { + onDidRemoveDecoration(callback: (event: Object) => void): Disposable { return this.emitter.on("did-remove-decoration", callback) } @@ -131,7 +131,7 @@ export default class DecorationManagement { * - decoration: the decoration object that was created * @return {Disposable} a disposable to stop listening to the event */ - onDidChangeDecoration(callback) { + onDidChangeDecoration(callback: (event: Object) => void): Disposable { return this.emitter.on("did-change-decoration", callback) } @@ -150,7 +150,7 @@ export default class DecorationManagement { * - decoration: the decoration object that was created * @return {Disposable} a disposable to stop listening to the event */ - onDidChangeDecorationRange(callback) { + onDidChangeDecorationRange(callback: (event: Object) => void): Disposable { return this.emitter.on("did-change-decoration-range", callback) } @@ -164,7 +164,7 @@ export default class DecorationManagement { * triggered * @return {Disposable} a disposable to stop listening to the event */ - onDidUpdateDecoration(callback) { + onDidUpdateDecoration(callback: (decoration: Decoration) => void): Disposable { return this.emitter.on("did-update-decoration", callback) } @@ -174,7 +174,7 @@ export default class DecorationManagement { * @param {number} id the decoration id * @return {Decoration} the decoration with the given id */ - decorationForId(id) { + decorationForId(id: number): Decoration { return this.decorationsById.get(id) } @@ -186,7 +186,7 @@ export default class DecorationManagement { * @return {Record} the decorations that intersect the passed-in * range */ - decorationsForScreenRowRange(startScreenRow, endScreenRow) { + decorationsForScreenRowRange(startScreenRow: number, endScreenRow: number): Record { const decorationsByMarkerId = {} const markers = this.findMarkers({ intersectsScreenRowRange: [startScreenRow, endScreenRow], @@ -231,7 +231,7 @@ export default class DecorationManagement { * highlight-outine decorations at a given * row */ - decorationsByTypeThenRows() { + decorationsByTypeThenRows(): {} { if (this.decorationsByTypeThenRowsCache != null) { return this.decorationsByTypeThenRowsCache } @@ -328,7 +328,10 @@ export default class DecorationManagement { * @emits {did-add-decoration} when the decoration is created successfully * @emits {did-change} when the decoration is created successfully */ - decorateMarker(marker, decorationParams) { + decorateMarker( + marker: Marker, + decorationParams: { class: string, color: string, plugin: string, render: function, scope: string, type: string } + ): Decoration { if (this.destroyed || this.minimap.destroyed || marker == null) { return } @@ -452,7 +455,7 @@ export default class DecorationManagement { * @param {Decoration} decoration the decoration for which emitting an event * @access private */ - emitDecorationChanges(type, decoration) { + emitDecorationChanges(type: string, decoration: Decoration) { if (this.destroyed || this.minimap.editorDestroyed()) { return } @@ -476,7 +479,7 @@ export default class DecorationManagement { * change object * @access private */ - emitRangeChanges(type, range, screenDelta) { + emitRangeChanges(type: string, range: {}, screenDelta: number) { const startScreenRow = range.start.row const endScreenRow = range.end.row const lastRenderedScreenRow = this.minimap.getLastVisibleScreenRow() @@ -503,7 +506,7 @@ export default class DecorationManagement { * @emits {did-change} when the decoration is removed * @emits {did-remove-decoration} when the decoration is removed */ - removeDecoration(decoration) { + removeDecoration(decoration: Decoration) { if (decoration == null) { return } @@ -555,7 +558,7 @@ export default class DecorationManagement { * @emits {did-change} when a decoration have been removed * @emits {did-remove-decoration} when a decoration have been removed */ - removeAllDecorationsForMarker(marker) { + removeAllDecorationsForMarker(marker: Marker) { if (marker == null) { return } @@ -586,7 +589,7 @@ export default class DecorationManagement { * @param {Marker} marker the marker for which removing decorations * @access private */ - removedAllMarkerDecorations(marker) { + removedAllMarkerDecorations(marker: Marker) { if (marker == null) { return } @@ -666,7 +669,7 @@ function getOriginatorPackageName() { * @return {Array} the array of diff ranges * @access private */ -function computeRangesDiffs(oldStart, oldEnd, newStart, newEnd) { +function computeRangesDiffs(oldStart: number, oldEnd: number, newStart: number, newEnd: number): Array<{}> { const diffs = [] if (oldStart.isLessThan(newStart)) { diff --git a/lib/decoration.js b/lib/decoration.js index 183e89b8..d59772d4 100644 --- a/lib/decoration.js +++ b/lib/decoration.js @@ -21,7 +21,7 @@ export default class Decoration { * @param {string} type the decoration type to match * @return {boolean} whether the decoration properties match the type */ - static isType(decorationProperties, type) { + static isType(decorationProperties: {}, type: string): boolean { if (Array.isArray(decorationProperties.type)) { if (decorationProperties.type.indexOf(type) >= 0) { return true @@ -40,7 +40,7 @@ export default class Decoration { * be displayed * @param {Object} properties the decoration's properties */ - constructor(marker, minimap, properties) { + constructor(marker: Marker, minimap: Minimap, properties: {}) { /** * @access private */ @@ -100,7 +100,7 @@ export default class Decoration { * * @return {boolean} whether this decoration is destroyed or not */ - isDestroyed() { + isDestroyed(): boolean { return this.destroyed } @@ -113,7 +113,7 @@ export default class Decoration { * when the event is triggered * @return {Disposable} a disposable to stop listening to the event */ - onDidChangeProperties(callback) { + onDidChangeProperties(callback: (change: {}) => void): Disposable { return this.emitter.on("did-change-properties", callback) } @@ -124,7 +124,7 @@ export default class Decoration { * is triggered * @return {Disposable} a disposable to stop listening to the event */ - onDidDestroy(callback) { + onDidDestroy(callback: () => void): Disposable { return this.emitter.on("did-destroy", callback) } @@ -133,7 +133,7 @@ export default class Decoration { * * @return {number} the decoration id */ - getId() { + getId(): number { return this.id } @@ -142,7 +142,7 @@ export default class Decoration { * * @return {Marker} the decoration's marker */ - getMarker() { + getMarker(): Marker { return this.marker } @@ -155,7 +155,7 @@ export default class Decoration { * matches any in the array. * @return {boolean} whether this decoration match the passed-in type */ - isType(type) { + isType(type: string | Array): boolean { return Decoration.isType(this.properties, type) } @@ -164,7 +164,7 @@ export default class Decoration { * * @return {Object} the decoration's properties */ - getProperties() { + getProperties(): {} { return this.properties } @@ -174,7 +174,7 @@ export default class Decoration { * * @param {Object} newProperties the new properties for the decoration */ - setProperties(newProperties) { + setProperties(newProperties: {}) { if (this.destroyed) { return } diff --git a/lib/dom-styles-reader.js b/lib/dom-styles-reader.js index e14cc323..383e93f0 100644 --- a/lib/dom-styles-reader.js +++ b/lib/dom-styles-reader.js @@ -47,7 +47,7 @@ export default class DOMStylesReader { * @return {string} the computed property's value * used in CanvasDrawer */ - retrieveStyleFromDom(scopes, property, targetNode, getFromCache) { + retrieveStyleFromDom(scopes: Array, property: string, targetNode: Node, getFromCache: boolean): string { if (!scopes.length) { return "" } // no scopes @@ -104,7 +104,7 @@ export default class DOMStylesReader { * * @access private */ - ensureDummyNodeExistence(targetNode) { + ensureDummyNodeExistence(targetNode: Node) { if (this.targetNode !== targetNode || this.dummyNode === undefined) { this.dummyNode = document.createElement("span") this.dummyNode.style.visibility = "hidden" @@ -159,7 +159,7 @@ const hueRegexp = /hue-rotate\((\d+)deg\)/ * @return {string} the rotated CSS color * @access private */ -function rotateHue(value, filter) { +function rotateHue(value: string, filter: string): string { const match = value.match(rgbExtractRegexp) let [, , r, g, b, , a] = match @@ -186,7 +186,7 @@ function rotateHue(value, filter) { * @return {Array} the rotated color channels * @access private */ -function rotate(r, g, b, angle) { +function rotate(r: number, g: number, b: number, angle: number): Array { const matrix = [1, 0, 0, 0, 1, 0, 0, 0, 1] const lumR = 0.2126 const lumG = 0.7152 diff --git a/lib/main.js b/lib/main.js index daa19e21..90339b6c 100644 --- a/lib/main.js +++ b/lib/main.js @@ -27,14 +27,14 @@ export { default as MinimapElement } from "./minimap-element" * @type {boolean} * @access private */ -let active = false +let active: boolean = false /** * The toggle state of the package. * * @type {boolean} * @access private */ -let toggled = false +let toggled: boolean = false /** * The `Map` where Minimap instances are stored with the text editor they * target as key. @@ -49,14 +49,14 @@ export let editorsMinimaps = null * @type {CompositeDisposable} * @access private */ -let subscriptions = null +let subscriptions: CompositeDisposable = null /** * The disposable that stores the package's commands subscription. * * @type {Disposable} * @access private */ -let subscriptionsOfCommands = null +let subscriptionsOfCommands: Disposable = null /** * The package's events emitter. @@ -188,7 +188,7 @@ export function toggle() { * * @param {string} template the name of the template to use */ -async function generatePlugin(template) { +async function generatePlugin(template: string) { const { default: MinimapPluginGeneratorElement } = await import("./minimap-plugin-generator-element") const view = new MinimapPluginGeneratorElement() view.template = template diff --git a/lib/minimap-element.js b/lib/minimap-element.js index 9adfefc8..5c2f9de4 100644 --- a/lib/minimap-element.js +++ b/lib/minimap-element.js @@ -409,7 +409,7 @@ class MinimapElement { * * @return {boolean} whether the MinimapElement is currently visible or not */ - isVisible() { + isVisible(): boolean { return this.offsetWidth > 0 || this.offsetHeight > 0 } @@ -422,7 +422,7 @@ class MinimapElement { * @param {HTMLElement} [parent] the DOM node where attaching the minimap * element */ - attach(parent) { + attach(parent: HTMLElement) { if (this.attached) { return } @@ -697,7 +697,7 @@ class MinimapElement { * get the DecorationManagement API for minimapElement * @return {DecorationManagement} */ - getDecorationManagement() { + getDecorationManagement(): DecorationManagement { return this.DecorationManagement } @@ -714,7 +714,7 @@ class MinimapElement { * * @return {Minimap} this element's Minimap */ - getModel() { + getModel(): Minimap { return this.minimap } @@ -724,7 +724,7 @@ class MinimapElement { * @param {Minimap} minimap the Minimap model for this instance. * @return {Minimap} this element's Minimap */ - setModel(minimap) { + setModel(minimap: Minimap): Minimap { this.minimap = minimap // set minimapElement for Minimap @@ -790,7 +790,7 @@ class MinimapElement { * * @param {boolean} standAlone the new mode for this MinimapElement */ - setStandAlone(standAlone) { + setStandAlone(standAlone: boolean) { this.standAlone = standAlone if (this.standAlone) { @@ -955,7 +955,7 @@ class MinimapElement { * @param {Boolean} displayCodeHighlights whether to render the code * highlights or not */ - setDisplayCodeHighlights(displayCodeHighlights) { + setDisplayCodeHighlights(displayCodeHighlights: Boolean) { this.displayCodeHighlights = displayCodeHighlights if (this.attached) { this.requestForcedUpdate() @@ -986,7 +986,7 @@ class MinimapElement { * @return {boolean} whether the visibility changed or not since the last call * @access private */ - checkForVisibilityChange() { + checkForVisibilityChange(): boolean { if (this.isVisible()) { if (this.wasVisible) { return false @@ -1015,7 +1015,7 @@ class MinimapElement { * were detected * @access private */ - measureHeightAndWidth(visibilityChanged, forceUpdate = true) { + measureHeightAndWidth(visibilityChanged: boolean, forceUpdate: [type] = true) { if (!this.minimap) { return } @@ -1199,7 +1199,7 @@ class MinimapElement { * @return {Disposable} a disposable to remove the media query listener * @access private */ - subscribeToMediaQuery() { + subscribeToMediaQuery(): Disposable { const mediaQuery = window.matchMedia("screen and (-webkit-min-device-pixel-ratio: 1.5)") const mediaListener = () => { this.requestForcedUpdate() @@ -1327,7 +1327,7 @@ export default minimapElement * @param {MouseEvent} mouseEvent the mouse event object * @access private */ -function extractMouseEventData(mouseEvent) { +function extractMouseEventData(mouseEvent: MouseEvent) { return { x: mouseEvent.pageX, y: mouseEvent.pageY, @@ -1346,7 +1346,7 @@ function extractMouseEventData(mouseEvent) { * @param {TouchEvent} touchEvent the touch event object * @access private */ -function extractTouchEventData(touchEvent) { +function extractTouchEventData(touchEvent: TouchEvent) { // Use the first touch on the target area. Other touches will be ignored in // case of multi-touch. const touch = touchEvent.changedTouches[0] @@ -1374,7 +1374,7 @@ function extractTouchEventData(touchEvent) { * @param {Object} styles the styles to apply * @access private */ -function applyStyles(element, styles) { +function applyStyles(element: HTMLElement, styles: {}) { if (!element) { return } @@ -1396,7 +1396,7 @@ function applyStyles(element, styles) { * @return {string} the CSS translation string * @access private */ -function makeTranslate(x = 0, y = 0, useHardwareAcceleration = false) { +function makeTranslate(x: number = 0, y: number = 0, useHardwareAcceleration: boolean = false): string { if (useHardwareAcceleration) { return `translate3d(${x}px, ${y}px, 0)` } else { @@ -1413,7 +1413,7 @@ function makeTranslate(x = 0, y = 0, useHardwareAcceleration = false) { * @return {string} the CSS scaling string * @access private */ -function makeScale(x = 0, y = x, useHardwareAcceleration = false) { +function makeScale(x: number = 0, y: number = x, useHardwareAcceleration: boolean = false): string { if (useHardwareAcceleration) { return `scale3d(${x}, ${y}, 1)` } else { @@ -1432,7 +1432,7 @@ function makeScale(x = 0, y = x, useHardwareAcceleration = false) { * @param {[type]} param.step the easing function for the animation * @access private */ -function animate({ from, to, duration, step }) { +function animate({ from, to, duration, step }: { duration: [type], from: [type], step: [type], to: [type] }) { const start = getTime() let progress @@ -1470,6 +1470,6 @@ function swing(progress) { * @return {Date} the current time as Date * @access private */ -function getTime() { +function getTime(): Date { return new Date() } diff --git a/lib/minimap.js b/lib/minimap.js index ea936d28..bf7795e2 100644 --- a/lib/minimap.js +++ b/lib/minimap.js @@ -27,7 +27,7 @@ export default class Minimap { * @param {number} [options.height] the minimap height in pixels * @throws {Error} Cannot create a minimap without an editor */ - constructor(options = {}) { + constructor(options: { height: number, standAlone: boolean, textEditor: TextEditor, width: number } = {}) { if (!options.textEditor) { throw new Error("Cannot create a minimap without an editor") } @@ -311,7 +311,7 @@ export default class Minimap { * * @return {boolean} whether this Minimap has been destroyed or not */ - isDestroyed() { + isDestroyed(): boolean { return this.destroyed } @@ -322,7 +322,7 @@ export default class Minimap { * @return void * @access private */ - scheduleChanges(changes) { + scheduleChanges(changes: Array) { this.pendingChangeEvents = this.pendingChangeEvents.concat(changes) // Optimisation: If the redraw delay is set to 0, do not even schedule a timer @@ -384,7 +384,7 @@ export default class Minimap { * after the change * @return {Disposable} a disposable to stop listening to the event */ - onDidChange(callback) { + onDidChange(callback: (event: Object) => void): Disposable { return this.emitter.on("did-change", callback) } @@ -395,7 +395,7 @@ export default class Minimap { * is triggered. * @return {Disposable} a disposable to stop listening to the event */ - onDidChangeConfig(callback) { + onDidChangeConfig(callback: () => void): Disposable { return this.emitter.on("did-change-config", callback) } @@ -413,7 +413,7 @@ export default class Minimap { * the callback. * @return {Disposable} a disposable to stop listening to the event */ - onDidChangeScrollTop(callback) { + onDidChangeScrollTop(callback: (minimap: Minimap) => void): Disposable { return this.emitter.on("did-change-scroll-top", callback) } @@ -427,7 +427,7 @@ export default class Minimap { * the callback. * @return {Disposable} a disposable to stop listening to the event */ - onDidChangeScrollLeft(callback) { + onDidChangeScrollLeft(callback: (minimap: Minimap) => void): Disposable { return this.emitter.on("did-change-scroll-left", callback) } @@ -444,7 +444,7 @@ export default class Minimap { * the callback. * @return {Disposable} a disposable to stop listening to the event */ - onDidChangeStandAlone(callback) { + onDidChangeStandAlone(callback: (minimap: Minimap) => void): Disposable { return this.emitter.on("did-change-stand-alone", callback) } @@ -459,7 +459,7 @@ export default class Minimap { * is triggered. * @return {Disposable} a disposable to stop listening to the event */ - onDidDestroy(callback) { + onDidDestroy(callback: () => void): Disposable { return this.emitter.on("did-destroy", callback) } @@ -469,7 +469,7 @@ export default class Minimap { * @return {Disposable} the disposable to dispose all the registered events * @access private */ - subscribeToConfig() { + subscribeToConfig(): Disposable { const subs = new CompositeDisposable() const opts = { scope: this.textEditor.getRootScopeDescriptor() } @@ -527,7 +527,7 @@ export default class Minimap { * * @return {boolean} whether this Minimap is in stand-alone mode or not. */ - isStandAlone() { + isStandAlone(): boolean { return this.standAlone } @@ -539,7 +539,7 @@ export default class Minimap { * @emits {did-change-stand-alone} if the stand-alone mode have been toggled * on or off by the call */ - setStandAlone(standAlone) { + setStandAlone(standAlone: boolean) { if (standAlone !== this.standAlone) { this.standAlone = standAlone this.emitter.emit("did-change-stand-alone", this) @@ -549,7 +549,7 @@ export default class Minimap { /** * @return {MinimapElement} returns the current minimapElement */ - getMinimapElement() { + getMinimapElement(): MinimapElement { return this.minimapElement } @@ -558,7 +558,7 @@ export default class Minimap { * * @return {TextEditor} this Minimap's text editor */ - getTextEditor() { + getTextEditor(): TextEditor { return this.textEditor } @@ -567,7 +567,7 @@ export default class Minimap { * * @return {TextEditorElement} the minimap's text editor element */ - getTextEditorElement() { + getTextEditorElement(): TextEditorElement { if (this.editorElement) { return this.editorElement } @@ -581,7 +581,7 @@ export default class Minimap { * * @return {number} the scaled height of the text editor */ - getTextEditorScaledHeight() { + getTextEditorScaledHeight(): number { return this.adapter.getHeight() * this.getVerticalScaleFactor() } @@ -590,7 +590,7 @@ export default class Minimap { * * @return {number} the scaled scroll top of the text editor */ - getTextEditorScaledScrollTop() { + getTextEditorScaledScrollTop(): number { return this.adapter.getScrollTop() * this.getVerticalScaleFactor() } @@ -599,7 +599,7 @@ export default class Minimap { * * @return {number} the scaled scroll left of the text editor */ - getTextEditorScaledScrollLeft() { + getTextEditorScaledScrollLeft(): number { return this.adapter.getScrollLeft() * this.getHorizontalScaleFactor() } @@ -612,7 +612,7 @@ export default class Minimap { * * @return {number} the maximum scroll top of the text editor */ - getTextEditorMaxScrollTop() { + getTextEditorMaxScrollTop(): number { return this.adapter.getMaxScrollTop() } @@ -621,7 +621,7 @@ export default class Minimap { * * @return {number} the scroll top of the text editor */ - getTextEditorScrollTop() { + getTextEditorScrollTop(): number { return this.adapter.getScrollTop() } @@ -630,7 +630,7 @@ export default class Minimap { * * @param {number} scrollTop the new scroll top value */ - setTextEditorScrollTop(scrollTop, ignoreTextEditorScroll = false) { + setTextEditorScrollTop(scrollTop: number, ignoreTextEditorScroll = false) { this.ignoreTextEditorScroll = ignoreTextEditorScroll this.adapter.setScrollTop(scrollTop) } @@ -640,7 +640,7 @@ export default class Minimap { * * @return {number} the scroll left of the text editor */ - getTextEditorScrollLeft() { + getTextEditorScrollLeft(): number { return this.adapter.getScrollLeft() } @@ -649,7 +649,7 @@ export default class Minimap { * * @return {number} the height of the text editor */ - getTextEditorHeight() { + getTextEditorHeight(): number { return this.adapter.getHeight() } @@ -663,7 +663,7 @@ export default class Minimap { * * @return {number} the scroll ratio of the text editor */ - getTextEditorScrollRatio() { + getTextEditorScrollRatio(): number { return this.adapter.getScrollTop() / (this.getTextEditorMaxScrollTop() || 1) } @@ -675,7 +675,7 @@ export default class Minimap { * @return {number} the scroll ratio of the text editor strictly between * 0 and 1 */ - getCapedTextEditorScrollRatio() { + getCapedTextEditorScrollRatio(): number { return Math.min(1, this.getTextEditorScrollRatio()) } @@ -685,7 +685,7 @@ export default class Minimap { * * @return {number} the height of the minimap */ - getHeight() { + getHeight(): number { return this.textEditor.getScreenLineCount() * this.getLineHeight() } @@ -695,7 +695,7 @@ export default class Minimap { * * @return {number} the width of the minimap */ - getWidth() { + getWidth(): number { return this.textEditor.getMaxScreenLineLength() * this.getCharWidth() } @@ -707,7 +707,7 @@ export default class Minimap { * * @return {number} the visible height of the Minimap */ - getVisibleHeight() { + getVisibleHeight(): number { return Math.min(this.getScreenHeight(), this.getHeight()) } @@ -718,7 +718,7 @@ export default class Minimap { * * @return {number} the total height of the Minimap */ - getScreenHeight() { + getScreenHeight(): number { if (this.isStandAlone()) { if (this.height != null) { return this.height @@ -735,7 +735,7 @@ export default class Minimap { * * @return {number} the width of the Minimap when displayed */ - getVisibleWidth() { + getVisibleWidth(): number { return Math.min(this.getScreenWidth(), this.getWidth()) } @@ -746,7 +746,7 @@ export default class Minimap { * * @return {number} the Minimap screen width */ - getScreenWidth() { + getScreenWidth(): number { if (this.isStandAlone() && this.width != null) { return this.width } else { @@ -763,7 +763,7 @@ export default class Minimap { * @param {number} height the new height of the Minimap * @param {number} width the new width of the Minimap */ - setScreenHeightAndWidth(height, width) { + setScreenHeightAndWidth(height: number, width: number) { if (this.width !== width || this.height !== height) { this.height = height this.width = width @@ -777,7 +777,7 @@ export default class Minimap { * * @return {number} the Minimap vertical scaling factor */ - getVerticalScaleFactor() { + getVerticalScaleFactor(): number { return this.getLineHeight() / this.textEditor.getLineHeightInPixels() } @@ -787,7 +787,7 @@ export default class Minimap { * * @return {number} the Minimap horizontal scaling factor */ - getHorizontalScaleFactor() { + getHorizontalScaleFactor(): number { return this.getCharWidth() / this.textEditor.getDefaultCharWidth() } @@ -796,7 +796,7 @@ export default class Minimap { * * @return {number} a line's height in the Minimap */ - getLineHeight() { + getLineHeight(): number { return this.getCharHeight() + this.getInterline() } @@ -805,7 +805,7 @@ export default class Minimap { * * @return {number} a character's width in the Minimap */ - getCharWidth() { + getCharWidth(): number { if (this.charWidth != null) { return this.charWidth } else { @@ -821,7 +821,7 @@ export default class Minimap { * @param {number} charWidth the new width of a char in the Minimap * @emits {did-change-config} when the value is changed */ - setCharWidth(charWidth) { + setCharWidth(charWidth: number) { this.charWidth = Math.floor(charWidth) this.emitter.emit("did-change-config") } @@ -831,7 +831,7 @@ export default class Minimap { * * @return {number} a character's height in the Minimap */ - getCharHeight() { + getCharHeight(): number { if (this.charHeight != null) { return this.charHeight } else { @@ -847,7 +847,7 @@ export default class Minimap { * @param {number} charHeight the new height of a char in the Minimap * @emits {did-change-config} when the value is changed */ - setCharHeight(charHeight) { + setCharHeight(charHeight: number) { this.charHeight = Math.floor(charHeight) this.emitter.emit("did-change-config") } @@ -857,7 +857,7 @@ export default class Minimap { * * @return {number} the interline's height in the Minimap */ - getInterline() { + getInterline(): number { if (this.interline != null) { return this.interline } else { @@ -873,7 +873,7 @@ export default class Minimap { * @param {number} interline the new height of an interline in the Minimap * @emits {did-change-config} when the value is changed */ - setInterline(interline) { + setInterline(interline: number) { this.interline = Math.floor(interline) this.emitter.emit("did-change-config") } @@ -883,7 +883,7 @@ export default class Minimap { * * @return {boolean} the devicePixelRatioRounding status in the Minimap */ - getDevicePixelRatioRounding() { + getDevicePixelRatioRounding(): boolean { if (this.devicePixelRatioRounding != null) { return this.devicePixelRatioRounding } else { @@ -901,7 +901,7 @@ export default class Minimap { * in the Minimap * @emits {did-change-config} when the value is changed */ - setDevicePixelRatioRounding(devicePixelRatioRounding) { + setDevicePixelRatioRounding(devicePixelRatioRounding: boolean) { this.devicePixelRatioRounding = devicePixelRatioRounding this.emitter.emit("did-change-config") } @@ -911,7 +911,7 @@ export default class Minimap { * * @return {number} the devicePixelRatio in the Minimap */ - getDevicePixelRatio() { + getDevicePixelRatio(): number { return this.getDevicePixelRatioRounding() ? Math.floor(devicePixelRatio) : devicePixelRatio } @@ -920,7 +920,7 @@ export default class Minimap { * * @return {number} the index of the first visible row */ - getFirstVisibleScreenRow() { + getFirstVisibleScreenRow(): number { return Math.floor(this.getScrollTop() / this.getLineHeight()) } @@ -929,7 +929,7 @@ export default class Minimap { * * @return {number} the index of the last visible row */ - getLastVisibleScreenRow() { + getLastVisibleScreenRow(): number { return Math.ceil((this.getScrollTop() + this.getScreenHeight()) / this.getLineHeight()) } @@ -938,7 +938,7 @@ export default class Minimap { * * @return {boolean} whether the minimap can scroll independently */ - scrollIndependentlyOnMouseWheel() { + scrollIndependentlyOnMouseWheel(): boolean { return this.independentMinimapScroll } @@ -950,7 +950,7 @@ export default class Minimap { * * @return {number} the scroll top of the Minimap */ - getScrollTop() { + getScrollTop(): number { return this.standAlone || this.independentMinimapScroll ? this.scrollTop : this.getScrollTopFromEditor() } @@ -960,7 +960,7 @@ export default class Minimap { * @param {number} scrollTop the new scroll top for the Minimap * @emits {did-change-scroll-top} if the Minimap's stand-alone mode is enabled */ - setScrollTop(scrollTop) { + setScrollTop(scrollTop: number) { this.scrollTop = Math.max(0, Math.min(this.getMaxScrollTop(), scrollTop)) if (this.standAlone || this.independentMinimapScroll) { @@ -973,7 +973,7 @@ export default class Minimap { * * @return {number} the minimap scroll ratio */ - getScrollRatio() { + getScrollRatio(): number { return this.getScrollTop() / this.getMaxScrollTop() } @@ -997,7 +997,7 @@ export default class Minimap { * * @return {number} the computed scroll top value */ - getScrollTopFromEditor() { + getScrollTopFromEditor(): number { return Math.abs(this.getCapedTextEditorScrollRatio() * this.getMaxScrollTop()) } @@ -1006,7 +1006,7 @@ export default class Minimap { * * @return {number} the maximum scroll top for the Minimap */ - getMaxScrollTop() { + getMaxScrollTop(): number { return Math.max(0, this.getHeight() - this.getScreenHeight()) } @@ -1015,7 +1015,7 @@ export default class Minimap { * * @return {boolean} whether this Minimap can scroll or not */ - canScroll() { + canScroll(): boolean { return this.getMaxScrollTop() > 0 } @@ -1026,7 +1026,7 @@ export default class Minimap { * @param {MouseEvent} event the mouse wheel event * @access private */ - onMouseWheel(event) { + onMouseWheel(event: MouseEvent) { if (this.scrollIndependentlyOnMouseWheel()) { event.stopPropagation() @@ -1080,7 +1080,7 @@ export default class Minimap { * @param {Object} changes a change to dispatch * @access private */ - emitChanges(changes) { + emitChanges(changes: {}) { this.emitter.emit("did-change", changes) } @@ -1111,7 +1111,7 @@ export default class Minimap { * get the DecorationManagement API for the current minimapElement * @return {DecorationManagement} */ - getDecorationManagement() { + getDecorationManagement(): DecorationManagement { if (!this.DecorationManagement) { if (this.minimapElement) { this.DecorationManagement = this.minimapElement.DecorationManagement diff --git a/lib/mixins/canvas-drawer.js b/lib/mixins/canvas-drawer.js index 5e8634cf..4664fcb2 100644 --- a/lib/mixins/canvas-drawer.js +++ b/lib/mixins/canvas-drawer.js @@ -89,7 +89,7 @@ export default class CanvasDrawer extends Mixin { * * @return {HTMLCanvasElement} the html canvas element */ - getFrontCanvas() { + getFrontCanvas(): HTMLCanvasElement { return this.frontLayer.canvas } @@ -99,7 +99,7 @@ export default class CanvasDrawer extends Mixin { * @param {HTMLElement} parent the canvases' container * @access private */ - attachCanvases(parent) { + attachCanvases(parent: HTMLElement) { this.backLayer.attach(parent) this.tokensLayer.attach(parent) this.frontLayer.attach(parent) @@ -112,7 +112,7 @@ export default class CanvasDrawer extends Mixin { * @param {number} height the new height for the three canvases * @access private */ - setCanvasesSize(width, height) { + setCanvasesSize(width: number, height: number) { this.backLayer.setSize(width, height) this.tokensLayer.setSize(width, height) this.frontLayer.setSize(width, height) @@ -324,22 +324,22 @@ export default class CanvasDrawer extends Mixin { * @access private */ function updateTokensLayer( - tokensLayer, - firstRow, - lastRow, - offscreenFirstRow, - offscreenLastRow, - pendingChanges, - lineHeight, - charHeight, - charWidth, - canvasWidth, - editor, + tokensLayer: CanvasLayer, + firstRow: number, + lastRow: number, + offscreenFirstRow: number, + offscreenLastRow: number, + pendingChanges: Array<>, + lineHeight: number, + charHeight: number, + charWidth: number, + canvasWidth: number, + editor: TextEditor, editorScreenLineCount, invisibleRegExp, - getTokenColorClosure, - ignoreWhitespacesInTokens, - maxTokensInOneLine + getTokenColorClosure: (t: Token) => string, + ignoreWhitespacesInTokens: boolean, + maxTokensInOneLine: number ) { // NOTE: this method is the hot function of Minimap. Do not refactor. The code is inlined delibarately. @@ -442,16 +442,16 @@ function updateTokensLayer( * @access private */ function updateBackDecorationsLayer( - backLayer, - firstRow, - lastRow, - offscreenFirstRow, - offscreenLastRow, - pendingBackDecorationChanges, - renderData, - lineHeight, - editorElement, - decorations + backLayer: CanvasLayer, + firstRow: number, + lastRow: number, + offscreenFirstRow: number, + offscreenLastRow: number, + pendingBackDecorationChanges: Array<>, + renderData: Object, + lineHeight: number, + editorElement: TextEditorElement, + decorations: Array ) { const intactRanges = computeIntactRanges( firstRow, @@ -531,16 +531,16 @@ function updateBackDecorationsLayer( * @access private */ function updateFrontDecorationsLayer( - frontLayer, - firstRow, - lastRow, - offscreenFirstRow, - offscreenLastRow, - pendingFrontDecorationChanges, - renderData, - lineHeight, - editorElement, - decorations + frontLayer: CanvasLayer, + firstRow: number, + lastRow: number, + offscreenFirstRow: number, + offscreenLastRow: number, + pendingFrontDecorationChanges: Array<>, + renderData: Object, + lineHeight: number, + editorElement: TextEditorElement, + decorations: Array ) { const intactRanges = computeIntactRanges( firstRow, @@ -618,7 +618,16 @@ const oneOrMoreWhiteSpaceRegexp = /\s+/ * @return {number} the x position at the end of the token * @access private */ -function drawToken(context, text, color, x, y, charWidth, charHeight, ignoreWhitespacesInTokens) { +function drawToken( + context: CanvasRenderingContext2D, + text: string, + color: string, + x: number, + y: number, + charWidth: number, + charHeight: number, + ignoreWhitespacesInTokens +): number { context.fillStyle = color if (ignoreWhitespacesInTokens) { @@ -671,20 +680,20 @@ function drawToken(context, text, color, x, y, charWidth, charHeight, ignoreWhit * @access private */ function drawLines( - firstRow, - lastRow, - offsetRow, - lineHeight, - charHeight, - charWidth, - canvasWidth, - context, - editor, - editorScreenLineCount, - invisibleRegExp, - getTokenColorClosure, - ignoreWhitespacesInTokens, - maxTokensInOneLine + firstRow: number, + lastRow: number, + offsetRow: number, + lineHeight: number, + charHeight: number, + charWidth: number, + canvasWidth: number, + context: CanvasRenderingContext2D, + editor: TextEditor, + editorScreenLineCount: number, + invisibleRegExp: RegExp, + getTokenColorClosure: (t: Token) => string, + ignoreWhitespacesInTokens: boolean, + maxTokensInOneLine: number ) { // NOTE: this method is the hot function of Minimap. Do not refactor. The code is inlined delibarately. @@ -744,7 +753,7 @@ function drawLines( * @return {RegExp} the regular expression to match invisible characters * @access private */ -function getInvisibleRegExp(editor) { +function getInvisibleRegExp(editor: TextEditor): RegExp { const invisibles = editor.getInvisibles() const regexp = [] if (invisibles.cr != null) { @@ -804,7 +813,7 @@ const frontDecorationDispatcher = { * @param {string} decorationColor decoration color * @access private */ -function drawLineDecoration(decoration, data, decorationColor) { +function drawLineDecoration(decoration: Decoration, data: {}, decorationColor: string) { const { context, lineHeight, canvasWidth, yRow } = data context.fillStyle = decorationColor @@ -819,7 +828,7 @@ function drawLineDecoration(decoration, data, decorationColor) { * @param {string} decorationColor decoration color * @access private */ -function drawGutterDecoration(decoration, data, decorationColor) { +function drawGutterDecoration(decoration: Decoration, data: {}, decorationColor: string) { const { context, lineHeight, yRow } = data context.fillStyle = decorationColor @@ -837,7 +846,7 @@ function drawGutterDecoration(decoration, data, decorationColor) { * @param {string} decorationColor decoration color * @access private */ -function drawHighlightDecoration(decoration, data, decorationColor) { +function drawHighlightDecoration(decoration: Decoration, data: {}, decorationColor: string) { const { context, lineHeight, charWidth, canvasWidth, screenRow, yRow } = data const range = decoration.getMarker().getScreenRange() @@ -869,7 +878,7 @@ function drawHighlightDecoration(decoration, data, decorationColor) { * @param {string} decorationColor decoration color * @access private */ -function drawHighlightOutlineDecoration(decoration, data, decorationColor) { +function drawHighlightOutlineDecoration(decoration: Decoration, data: {}, decorationColor: string) { const { context, lineHeight, charWidth, canvasWidth, screenRow, yRow } = data let bottomWidth, colSpan, width, xBottomStart, xEnd, xStart @@ -952,7 +961,12 @@ function drawHighlightOutlineDecoration(decoration, data, decorationColor) { * @param {TextEditorElement} editorElement * @access private */ -function drawCustomDecoration(decoration, data, decorationColor, editorElement) { +function drawCustomDecoration( + decoration: Decoration, + data: {}, + decorationColor: string, + editorElement: TextEditorElement +) { const renderRoutine = decoration.getProperties().render if (renderRoutine) { @@ -976,7 +990,13 @@ function drawCustomDecoration(decoration, data, decorationColor, editorElement) * @param {TextEditorElement} editorElement * @access private */ -function drawDecorations(screenRow, decorations, renderData, types, editorElement) { +function drawDecorations( + screenRow: number, + decorations: {}, + renderData: {}, + types: {}, + editorElement: TextEditorElement +) { let decorationsToRender = [] renderData.context.clearRect(0, renderData.yRow, renderData.canvasWidth, renderData.lineHeight) @@ -1027,13 +1047,13 @@ function drawDecorations(screenRow, decorations, renderData, types, editorElemen * @access private */ function drawFrontDecorationsForLines( - firstRow, - lastRow, - offsetRow, - renderData, - lineHeight, - editorElement, - decorations + firstRow: number, + lastRow: number, + offsetRow: number, + renderData: {}, + lineHeight: number, + editorElement: TextEditorElement, + decorations: Array ) { if (firstRow > lastRow) { return @@ -1067,7 +1087,15 @@ function drawFrontDecorationsForLines( * @param {Array} decorations * @access private */ -function drawBackDecorationsForLines(firstRow, lastRow, offsetRow, renderData, lineHeight, editorElement, decorations) { +function drawBackDecorationsForLines( + firstRow: number, + lastRow: number, + offsetRow: number, + renderData: {}, + lineHeight: number, + editorElement: TextEditorElement, + decorations: Array +) { if (firstRow > lastRow) { return } @@ -1107,7 +1135,7 @@ function drawBackDecorationsForLines(firstRow, lastRow, offsetRow, renderData, l * @param {number} textOpacity * @return {string} a CSS color */ -function getDefaultColor(editorElement, textOpacity) { +function getDefaultColor(editorElement: TextEditorElement, textOpacity: number): string { const color = domStylesReader.retrieveStyleFromDom([".editor"], "color", editorElement, true) return transparentize(color, textOpacity) } @@ -1123,7 +1151,7 @@ function getDefaultColor(editorElement, textOpacity) { * @param {number} textOpacity * @return {string} the CSS color for the provided token */ -function getTokenColor(scopes, editorElement, textOpacity) { +function getTokenColor(scopes: Array, editorElement: TextEditorElement, textOpacity: number): string { const color = domStylesReader.retrieveStyleFromDom(scopes, "color", editorElement, true) return transparentize(color, textOpacity) @@ -1138,7 +1166,7 @@ function getTokenColor(scopes, editorElement, textOpacity) { * @return {string} the transparentized CSS color * @access private */ -function transparentize(color, opacity) { +function transparentize(color: string, opacity: number): string { // assumes that color is in form of `rgb(content)` with no spaces around the given value return `rgba(${color.slice(4, -1)}, ${opacity})` } @@ -1154,7 +1182,7 @@ function transparentize(color, opacity) { * @param {TextEditorElement} editorElement * @return {string} the CSS color for the provided decoration */ -function getDecorationColor(decoration, editorElement) { +function getDecorationColor(decoration: Decoration, editorElement: TextEditorElement): string { const properties = decoration.getProperties() if (properties.color) { return properties.color @@ -1186,7 +1214,13 @@ function getDecorationColor(decoration, editorElement) { * @return {Array} the intact ranges in the rendered region * @access private */ -function computeIntactRanges(firstRow, lastRow, changes, offscreenFirstRow, offscreenLastRow) { +function computeIntactRanges( + firstRow: number, + lastRow: number, + changes, + offscreenFirstRow: number | null, + offscreenLastRow: number | null +): Array<{}> { // TODO when do they get null? if (offscreenFirstRow == null && offscreenLastRow == null) { return [] @@ -1277,7 +1311,7 @@ function computeIntactRanges(firstRow, lastRow, changes, offscreenFirstRow, offs * @return {Array} the array of truncated ranges * @access private */ -function truncateIntactRanges(intactRanges, firstRow, lastRow) { +function truncateIntactRanges(intactRanges: Array<{}>, firstRow: number, lastRow: number): Array<{}> { let i = 0 while (i < intactRanges.length) { const range = intactRanges[i] diff --git a/lib/plugin-management.js b/lib/plugin-management.js index 7283ae50..76f99d8a 100644 --- a/lib/plugin-management.js +++ b/lib/plugin-management.js @@ -34,7 +34,7 @@ export const plugins = {} * @type {Object} * @access private */ -const pluginsSubscriptions = {} +const pluginsSubscriptions: {} = {} /** * A map that stores the display order for each plugin @@ -42,7 +42,7 @@ const pluginsSubscriptions = {} * @type {Object} * @access private */ -const pluginsOrderMap = {} +const pluginsOrderMap: {} = {} /** * Registers a minimap `plugin` with the given `name`. @@ -144,7 +144,7 @@ function* eachPlugin() { * @emits {did-deactivate-plugin} if the plugin was deactivated by the call. * @access private */ -function updatesPluginActivationState(name) { +function updatesPluginActivationState(name: string) { const plugin = plugins[name] const pluginActive = plugin.isActive() const settingActive = atom.config.get(`minimap.plugins.${name}`) @@ -192,7 +192,7 @@ export function deactivatePlugin(name, plugin) { * to toggle the plugin state. * @access private */ -function registerPluginControls(name, plugin) { +function registerPluginControls(name: string, plugin: MinimapPlugin) { const settingsKey = `minimap.plugins.${name}` const orderSettingsKey = `minimap.plugins.${name}DecorationsZIndex` @@ -251,7 +251,7 @@ function registerPluginControls(name, plugin) { * @param {string} name the name of the plugin to update * @access private */ -function updatePluginsOrderMap(name) { +function updatePluginsOrderMap(name: string) { const orderSettingsKey = `minimap.plugins.${name}DecorationsZIndex` pluginsOrderMap[name] = atom.config.get(orderSettingsKey) @@ -274,7 +274,7 @@ export function getPluginsOrder() { * @param {string} name The identifying name of the plugin. * @access private */ -function unregisterPluginControls(name) { +function unregisterPluginControls(name: string) { pluginsSubscriptions[name].dispose() delete pluginsSubscriptions[name] delete getConfigSchema().plugins.properties[name]