|
| 1 | +/** |
| 2 | + * ## Diff extension |
| 3 | + * |
| 4 | + * Provides side-by-side and inline diff view capabilities for comparing code differences between two versions. |
| 5 | + * Supports visual highlighting of additions, deletions, and modifications with customizable diff providers |
| 6 | + * and rendering options. Includes features for synchronized scrolling, line number alignment, and |
| 7 | + * various diff computation algorithms. |
| 8 | + * |
| 9 | + * **Components:** |
| 10 | + * - `InlineDiffView`: Single editor view showing changes inline with markers |
| 11 | + * - `SplitDiffView`: Side-by-side comparison view with two synchronized editors |
| 12 | + * - `DiffProvider`: Configurable algorithms for computing differences |
| 13 | + * |
| 14 | + * **Usage:** |
| 15 | + * ```javascript |
| 16 | + * const diffView = createDiffView({ |
| 17 | + * valueA: originalContent, |
| 18 | + * valueB: modifiedContent, |
| 19 | + * inline: false // or 'a'/'b' for inline view |
| 20 | + * }); |
| 21 | + * ``` |
| 22 | + * |
| 23 | + * @module |
| 24 | + */ |
| 25 | + |
| 26 | +var InlineDiffView = require("./diff/inline_diff_view").InlineDiffView; |
| 27 | +var SplitDiffView = require("./diff/split_diff_view").SplitDiffView; |
| 28 | +var DiffProvider = require("./diff/providers/default").DiffProvider; |
| 29 | + |
| 30 | +/** |
| 31 | + * Interface representing a model for handling differences between two views or states. |
| 32 | + * @typedef {Object} DiffModel |
| 33 | + * @property {import("../editor").Editor} [editorA] - The editor for the original view. |
| 34 | + * @property {import("../editor").Editor} [editorB] - The editor for the edited view. |
| 35 | + * @property {import("../edit_session").EditSession} [sessionA] - The edit session for the original view. |
| 36 | + * @property {import("../edit_session").EditSession} [sessionB] - The edit session for the edited view. |
| 37 | + * @property {string} [valueA] - The original content. |
| 38 | + * @property {string} [valueB] - The modified content. |
| 39 | + * @property {"a"|"b"} [inline] - Whether to show the original view("a") or modified view("b") for inline diff view |
| 40 | + * @property {IDiffProvider} [diffProvider] - Provider for computing differences between original and modified content. |
| 41 | + */ |
| 42 | + |
| 43 | +/** |
| 44 | + * @typedef {Object} DiffViewOptions |
| 45 | + * @property {boolean} [showOtherLineNumbers=true] - Whether to show line numbers in the other editor's gutter |
| 46 | + * @property {boolean} [folding] - Whether to enable code folding widgets |
| 47 | + * @property {boolean} [syncSelections] - Whether to synchronize selections between both editors |
| 48 | + * @property {boolean} [ignoreTrimWhitespace] - Whether to ignore trimmed whitespace when computing diffs |
| 49 | + * @property {boolean} [wrap] - Whether to enable word wrapping in both editors |
| 50 | + * @property {number} [maxDiffs=5000] - Maximum number of diffs to compute before failing silently |
| 51 | + * @property {string|import("../../ace-internal").Ace.Theme} [theme] - Theme to apply to both editors |
| 52 | + */ |
| 53 | + |
| 54 | +/** |
| 55 | + * @typedef {Object} IDiffProvider |
| 56 | + * @property {(originalLines: string[], modifiedLines: string[], opts?: any) => import("./diff/base_diff_view").DiffChunk[]} compute - Computes differences between original and modified lines |
| 57 | + */ |
| 58 | + |
| 59 | + |
| 60 | +/** |
| 61 | + * Creates a diff view for comparing code. |
| 62 | + * @param {DiffModel} [diffModel] model for the diff view |
| 63 | + * @param {DiffViewOptions} [options] options for the diff view |
| 64 | + * @returns {InlineDiffView|SplitDiffView} Configured diff view instance |
| 65 | + */ |
| 66 | +function createDiffView(diffModel, options) { |
| 67 | + diffModel = diffModel || {}; |
| 68 | + diffModel.diffProvider = diffModel.diffProvider || new DiffProvider(); //use default diff provider; |
| 69 | + let diffView; |
| 70 | + if (diffModel.inline) { |
| 71 | + diffView = new InlineDiffView(diffModel); |
| 72 | + } |
| 73 | + else { |
| 74 | + diffView = new SplitDiffView(diffModel); |
| 75 | + } |
| 76 | + if (options) { |
| 77 | + diffView.setOptions(options); |
| 78 | + } |
| 79 | + |
| 80 | + return diffView; |
| 81 | +} |
| 82 | + |
| 83 | +exports.InlineDiffView = InlineDiffView; |
| 84 | +exports.SplitDiffView = SplitDiffView; |
| 85 | +exports.DiffProvider = DiffProvider; |
| 86 | +exports.createDiffView = createDiffView; |
0 commit comments