Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve types for internal development #5662

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
262 changes: 150 additions & 112 deletions ace-internal.d.ts

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions src/ace.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ exports.config = require("./config");

/**
* Embeds the Ace editor into the DOM, at the element provided by `el`.
* @param {String | HTMLElement & {env?, value?}} el Either the id of an element, or the element itself
* @param {String | HTMLElement & {env?: any, value?: any} | null} [el] Either the id of an element, or the element itself
* @param {Object } [options] Options for the editor
* @returns {Editor}
**/
Expand Down Expand Up @@ -86,4 +86,5 @@ exports.Editor = Editor;
exports.EditSession = EditSession;
exports.UndoManager = UndoManager;
exports.VirtualRenderer = Renderer;
exports.version = exports.config.version;
var version = exports.config.version;
exports.version = version;
7 changes: 4 additions & 3 deletions src/anchor.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Anchor {
else
this.setPosition(row, column);
}

/**
* Returns an object identifying the `row` and `column` position of the current anchor.
* @returns {import("../ace-internal").Ace.Point}
Expand All @@ -42,18 +42,19 @@ class Anchor {
getDocument() {
return this.document;
}

/**
* Internal function called when `"change"` event fired.
* @param {import("../ace-internal").Ace.Delta} delta
* @internal
*/
onChange(delta) {
if (delta.start.row == delta.end.row && delta.start.row != this.row)
return;

if (delta.start.row > this.row)
return;

var point = $getTransformedPoint(delta, {row: this.row, column: this.column}, this.$insertRight);
this.setPosition(point.row, point.column, true);
}
Expand Down
71 changes: 45 additions & 26 deletions src/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* @typedef {import("./editor").Editor} Editor
* @typedef {import("../ace-internal").Ace.CompletionProviderOptions} CompletionProviderOptions
* @typedef {import("../ace-internal").Ace.CompletionOptions} CompletionOptions
* @typedef {import("../ace-internal").Ace.Position} Position
*/
var HashHandler = require("./keyboard/hash_handler").HashHandler;
var AcePopup = require("./autocomplete/popup").AcePopup;
Expand Down Expand Up @@ -31,7 +32,7 @@
* @property {string} [command] - A command to be executed after the completion is inserted (experimental)
* @property {string} [snippet] - a text snippet that would be inserted when the completion is selected
* @property {string} [value] - The text that would be inserted when selecting this completion.
* @property {import("../ace-internal").Ace.Completer & {insertMatch:(editor: Editor, data: Completion) => void}} [completer]
* @property {import("../ace-internal").Ace.Completer} [completer]
* @property {boolean} [hideInlinePreview]
* @export
*/
Expand Down Expand Up @@ -76,10 +77,11 @@
this.keyboardHandler.bindKeys(this.commands);
this.parentNode = null;
this.setSelectOnHover = false;
/**@private*/
this.hasSeen = new Set();

/**
* @property {Boolean} showLoadingState - A boolean indicating whether the loading states of the Autocompletion should be shown to the end-user. If enabled
* @property {Boolean} showLoadingState - A boolean indicating whether the loading states of the Autocompletion should be shown to the end-user. If enabled
* it shows a loading indicator on the popup while autocomplete is loading.
*
* Experimental: This visualisation is not yet considered stable and might change in the future.
Expand Down Expand Up @@ -255,6 +257,10 @@
});
this.$elements = null;
}

/**
* @internal
*/
onLayoutChange() {
if (!this.popup.isOpen) return this.unObserveLayoutChanges();
this.$updatePopupPosition();
Expand Down Expand Up @@ -470,7 +476,7 @@
/**
* This is the entry point for the autocompletion class, triggers the actions which collect and display suggestions
* @param {Editor} editor
* @param {CompletionOptions} options
* @param {CompletionOptions} [options]
*/
showPopup(editor, options) {
if (this.editor)
Expand All @@ -493,6 +499,11 @@
this.updateCompletions(false, options);
}

/**
*
* @param {{pos: Position, prefix: string}} [initialPosition]
* @return {CompletionProvider}
*/
getCompletionProvider(initialPosition) {
if (!this.completionProvider)
this.completionProvider = new CompletionProvider(initialPosition);
Expand All @@ -510,7 +521,7 @@

/**
* @param {boolean} keepPopupPosition
* @param {CompletionOptions} options
* @param {CompletionOptions} [options]
*/
updateCompletions(keepPopupPosition, options) {
if (keepPopupPosition && this.base && this.completions) {
Expand Down Expand Up @@ -705,7 +716,11 @@
if (el.parentNode)
el.parentNode.removeChild(el);
}


/**
* @param e
* @internal
*/
onTooltipClick(e) {
var a = e.target;
while (a && a != this.tooltipNode) {
Expand Down Expand Up @@ -733,6 +748,30 @@
this.inlineRenderer = this.popup = this.editor = null;
}

/**
* @param {Editor} editor
* @return {Autocomplete}
*/
static for(editor) {
if (editor.completer instanceof Autocomplete) {
return editor.completer;
}
if (editor.completer) {
editor.completer.destroy();
editor.completer = null;
}
if (config.get("sharedPopups")) {
if (!Autocomplete["$sharedInstance"])
Autocomplete["$sharedInstance"] = new Autocomplete();
editor.completer = Autocomplete["$sharedInstance"];

Check warning on line 766 in src/autocomplete.js

View check run for this annotation

Codecov / codecov/patch

src/autocomplete.js#L764-L766

Added lines #L764 - L766 were not covered by tests
} else {
editor.completer = new Autocomplete();
editor.once("destroy", destroyCompleter);
}
// @ts-expect-error
return editor.completer;
}

}

Autocomplete.prototype.commands = {
Expand Down Expand Up @@ -762,26 +801,6 @@
"PageDown": function(editor) { editor.completer.popup.gotoPageDown(); }
};


Autocomplete.for = function(editor) {
if (editor.completer instanceof Autocomplete) {
return editor.completer;
}
if (editor.completer) {
editor.completer.destroy();
editor.completer = null;
}
if (config.get("sharedPopups")) {
if (!Autocomplete["$sharedInstance"])
Autocomplete["$sharedInstance"] = new Autocomplete();
editor.completer = Autocomplete["$sharedInstance"];
} else {
editor.completer = new Autocomplete();
editor.once("destroy", destroyCompleter);
}
return editor.completer;
};

Autocomplete.startCommand = {
name: "startAutocomplete",
exec: function(editor, options) {
Expand All @@ -803,7 +822,7 @@


/**
* @param {{pos: import("../ace-internal").Ace.Position, prefix: string}} initialPosition
* @param {{pos: Position, prefix: string}} [initialPosition]
*/
constructor(initialPosition) {
this.initialPosition = initialPosition;
Expand Down
28 changes: 16 additions & 12 deletions src/bidihandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
this.isMoveLeftOperation = false;
this.seenBidi = bidiRE.test(session.getValue());
}

/**
* Returns 'true' if row contains Bidi characters, in such case
* creates Bidi map to be used in operations related to selection
Expand All @@ -59,13 +59,17 @@
return this.bidiMap.bidiLevels;
}

/**
* @param {import("../ace-internal").Ace.Delta} delta
* @internal
*/
onChange(delta) {
if (!this.seenBidi) {
if (delta.action == "insert" && bidiRE.test(delta.lines.join("\n"))) {
this.seenBidi = true;
this.currentRow = null;
}
}
}
else {
this.currentRow = null;
}
Expand Down Expand Up @@ -106,7 +110,7 @@
updateRowLine(docRow, splitIndex) {
if (docRow === undefined)
docRow = this.getDocumentRow();

var isLastRow = (docRow === this.session.getLength() - 1),
endOfLine = isLastRow ? this.EOF : this.EOL;

Expand Down Expand Up @@ -136,7 +140,7 @@
} else {
this.line += this.showInvisibles ? endOfLine : bidiUtil.DOT;
}

/* replace tab and wide characters by commensurate spaces */
var session = this.session, shift = 0, size;
this.line = this.line.replace(/\t|[\u1100-\u2029, \u202F-\uFFE6]/g, function(ch, i){
Expand All @@ -153,7 +157,7 @@
this.rtlLineOffset = this.contentWidth - this.fontMetrics.$main.getBoundingClientRect().width;
}
}

updateBidiMap() {
var textCharTypes = [];
if (bidiUtil.hasBidiCharacters(this.line, textCharTypes) || this.isRtlDir) {
Expand Down Expand Up @@ -197,7 +201,7 @@
}

setEolChar(eolChar) {
this.EOL = eolChar;
this.EOL = eolChar;
}

setContentWidth(width) {
Expand All @@ -209,11 +213,11 @@
if (row != undefined)
return (this.session.getLine(row).charAt(0) == this.RLE);
else
return this.isRtlDir;
return this.isRtlDir;

Check warning on line 216 in src/bidihandler.js

View check run for this annotation

Codecov / codecov/patch

src/bidihandler.js#L216

Added line #L216 was not covered by tests
}

setRtlDirection(editor, isRtlDir) {
var cursor = editor.getCursorPosition();
var cursor = editor.getCursorPosition();

Check warning on line 220 in src/bidihandler.js

View check run for this annotation

Codecov / codecov/patch

src/bidihandler.js#L220

Added line #L220 was not covered by tests
for (var row = editor.selection.getSelectionAnchor().row; row <= cursor.row; row++) {
if (!isRtlDir && editor.session.getLine(row).charAt(0) === editor.session.$bidiHandler.RLE)
editor.session.doc.removeInLine(row, 0, 1);
Expand All @@ -238,7 +242,7 @@

if (!this.session.getOverwrite() && col <= leftBoundary && levels[visualIdx] % 2 !== 0)
visualIdx++;

for (var i = 0; i < visualIdx; i++) {
left += this.charWidths[levels[i]];
}
Expand Down Expand Up @@ -266,7 +270,7 @@
var map = this.bidiMap, levels = map.bidiLevels, level, selections = [], offset = 0,
selColMin = Math.min(startCol, endCol) - this.wrapIndent, selColMax = Math.max(startCol, endCol) - this.wrapIndent,
isSelected = false, isSelectedPrev = false, selectionStart = 0;

if (this.wrapIndent)
offset += this.isRtlDir ? (-1 * this.wrapOffset) : this.wrapOffset;

Expand Down Expand Up @@ -311,7 +315,7 @@

if (this.wrapIndent)
posX -= this.isRtlDir ? (-1 * this.wrapOffset) : this.wrapOffset;

while(posX > offset + charWidth/2) {
offset += charWidth;
if(visualIdx === levels.length - 1) {
Expand All @@ -321,7 +325,7 @@
}
charWidth = this.charWidths[levels[++visualIdx]];
}

if (visualIdx > 0 && (levels[visualIdx - 1] % 2 !== 0) && (levels[visualIdx] % 2 === 0)){
/* Bidi character on the left and None Bidi character on the right */
if(posX < offset)
Expand Down
16 changes: 8 additions & 8 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var AppConfig = require("./lib/app_config").AppConfig;

module.exports = exports = new AppConfig();

/** @type {import("../ace-internal").Ace.ConfigOptions} */
var options = {
packaged: false,
workerPath: null,
Expand All @@ -20,8 +21,9 @@ var options = {
};

/**
* @param {string} key
* @return {*}
* @template {keyof import("../ace-internal").Ace.ConfigOptions} K
* @param {K} key - The key of the config option to retrieve.
* @returns {import("../ace-internal").Ace.ConfigOptions[K]} - The value of the config option.
*/
exports.get = function(key) {
if (!options.hasOwnProperty(key))
Expand All @@ -30,8 +32,9 @@ exports.get = function(key) {
};

/**
* @param {string} key
* @param value
* @template {keyof import("../ace-internal").Ace.ConfigOptions} K
* @param {K} key
* @param {import("../ace-internal").Ace.ConfigOptions[K]} value
*/
exports.set = function(key, value) {
if (options.hasOwnProperty(key))
Expand All @@ -42,7 +45,7 @@ exports.set = function(key, value) {
dom.useStrictCSP(value);
};
/**
* @return {{[key: string]: any}}
* @return {import("../ace-internal").Ace.ConfigOptions}
*/
exports.all = function() {
return lang.copyObject(options);
Expand Down Expand Up @@ -100,9 +103,6 @@ var loader = function(moduleName, cb) {
console.error("loader is not configured");
};
var customLoader;
/**
* @param {(moduleName: string, afterLoad: (err: Error | null, module: unknown) => void) => void}cb
*/
exports.setLoader = function(cb) {
customLoader = cb;
};
Expand Down
Loading
Loading