Skip to content
Open
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
6 changes: 5 additions & 1 deletion src/components/composer/composer/abstract_composer_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,11 +324,15 @@ export abstract class AbstractComposerStore extends SpreadsheetStore {
hoveredFormula = `=${hoveredFormula}`;
}
const canonicalFormula = canonicalizeNumberContent(hoveredFormula, this.getters.getLocale());
const result = this.getters.evaluateFormulaResult(this.sheetId, canonicalFormula);
const result = this.evaluateCanonicalFormula(canonicalFormula);
this.hoveredTokens = hoveredContextTokens;
this.hoveredContentEvaluation = this.evaluationResultToDisplayString(result);
}

protected evaluateCanonicalFormula(canonicalFormula: string) {
return this.getters.evaluateFormulaResult(this.sheetId, canonicalFormula);
}

private getRelatedTokens(tokens: EnrichedToken[], tokenIndex: number): EnrichedToken[] {
try {
const ast = parseTokens(tokens);
Expand Down
14 changes: 13 additions & 1 deletion src/components/composer/composer/cell_composer_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,11 @@ export class CellComposerStore extends AbstractComposerStore {
return;
}

const evaluated = this.getters.evaluateFormula(this.sheetId, content);
const evaluated = this.getters.evaluateFormula(this.sheetId, content, {
sheetId: this.sheetId,
col: this.col,
row: this.row,
});
if (!isMatrix(evaluated)) {
return;
}
Expand Down Expand Up @@ -295,4 +299,12 @@ export class CellComposerStore extends AbstractComposerStore {
}
return true;
}

protected evaluateCanonicalFormula(canonicalFormula: string) {
return this.getters.evaluateFormulaResult(this.sheetId, canonicalFormula, {
sheetId: this.sheetId,
col: this.col,
row: this.row,
});
}
}
5 changes: 2 additions & 3 deletions src/components/composer/composer/composer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ import { AutoCompleteStore } from "../autocomplete_dropdown/autocomplete_dropdow
import { ContentEditableHelper } from "../content_editable_helper";
import { FunctionDescriptionProvider } from "../formula_assistant/formula_assistant";
import { SpeechBubble } from "../speech_bubble/speech_bubble";
import { DEFAULT_TOKEN_COLOR } from "./abstract_composer_store";
import { CellComposerStore } from "./cell_composer_store";
import { AbstractComposerStore, DEFAULT_TOKEN_COLOR } from "./abstract_composer_store";

const functions = functionRegistry.content;

Expand Down Expand Up @@ -133,7 +132,7 @@ export interface CellComposerProps {
onComposerCellFocused?: (content: String) => void;
onInputContextMenu?: (event: MouseEvent) => void;
isDefaultFocus?: boolean;
composerStore: Store<CellComposerStore>;
composerStore: Store<AbstractComposerStore>;
placeholder?: string;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,12 @@ export class StandaloneComposerStore extends AbstractComposerStore {
}
return super.getTokenColor(token);
}

hoverToken() {
/**
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry about this, the thing is I wanted to limit the mutators of StandaloneComposerStore but the typing of the mutators prevents it :'(

* Some functions can only be evaluated in the context of the grid, which is not feasible
* in the standalone composer.
*/
return;
}
}
13 changes: 9 additions & 4 deletions src/plugins/ui_core_views/cell_evaluation/evaluation_plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,12 @@ export class EvaluationPlugin extends CoreViewPlugin {
// Getters
// ---------------------------------------------------------------------------

evaluateFormula(sheetId: UID, formulaString: string): CellValue | Matrix<CellValue> {
const result = this.evaluateFormulaResult(sheetId, formulaString);
evaluateFormula(
sheetId: UID,
formulaString: string,
cellPosition?: CellPosition
): CellValue | Matrix<CellValue> {
const result = this.evaluateFormulaResult(sheetId, formulaString, cellPosition);
if (isMatrix(result)) {
return matrixMap(result, (cell) => cell.value);
}
Expand All @@ -227,9 +231,10 @@ export class EvaluationPlugin extends CoreViewPlugin {

evaluateFormulaResult(
sheetId: UID,
formulaString: string
formulaString: string,
cellPosition?: CellPosition
): Matrix<FunctionResultObject> | FunctionResultObject {
return this.evaluator.evaluateFormulaResult(sheetId, formulaString);
return this.evaluator.evaluateFormulaResult(sheetId, formulaString, cellPosition);
}

evaluateCompiledFormula(
Expand Down
21 changes: 19 additions & 2 deletions src/plugins/ui_core_views/cell_evaluation/evaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,22 @@ export class Evaluator {
}
}

private updateCompilationParametersForIsolatedFormula(originCellPosition?: CellPosition) {
this.compilationParams = buildCompilationParameters(
this.context,
this.getters,
this.computeAndSave.bind(this)
);
this.compilationParams.evalContext.__originCellPosition = originCellPosition;
this.compilationParams.evalContext.updateDependencies = undefined;
this.compilationParams.evalContext.addDependencies = undefined;
this.compilationParams.evalContext.lookupCaches = this.compilationParams.evalContext
.lookupCaches || {
forwardSearch: new Map(),
reverseSearch: new Map(),
};
}

private updateCompilationParameters() {
// rebuild the compilation parameters (with a clean cache)
this.compilationParams = buildCompilationParameters(
Expand Down Expand Up @@ -217,14 +233,15 @@ export class Evaluator {

evaluateFormulaResult(
sheetId: UID,
formulaString: string
formulaString: string,
originCellPosition?: CellPosition
): FunctionResultObject | Matrix<FunctionResultObject> {
const compiledFormula = compile(formulaString);

const ranges: Range[] = compiledFormula.dependencies.map((xc) =>
this.getters.getRangeFromSheetXC(sheetId, xc)
);
this.updateCompilationParameters();
this.updateCompilationParametersForIsolatedFormula(originCellPosition);
return this.evaluateCompiledFormula(sheetId, {
...compiledFormula,
dependencies: ranges,
Expand Down
47 changes: 46 additions & 1 deletion tests/composer/composer_hover.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { SpreadsheetChildEnv } from "../../src";
import { CellComposerStore } from "../../src/components/composer/composer/cell_composer_store";
import { toZone } from "../../src/helpers";
import { Model } from "../../src/model";
import { Store } from "../../src/store_engine";
import { setCellContent, setFormat, updateLocale } from "../test_helpers/commands_helpers";
import { FR_LOCALE } from "../test_helpers/constants";
import { getElStyle, keyDown, triggerMouseEvent } from "../test_helpers/dom_helper";
import { click, getElStyle, keyDown, triggerMouseEvent } from "../test_helpers/dom_helper";
import { getEvaluatedCell } from "../test_helpers/getters_helpers";
import {
ComposerWrapper,
editStandaloneComposer,
mountComposerWrapper,
mountSpreadsheet,
nextTick,
toRangeData,
typeInComposerGrid,
typeInComposerHelper,
} from "../test_helpers/helpers";
Expand Down Expand Up @@ -363,4 +366,46 @@ describe("Composer hover integration test", () => {
expect(".o-grid-composer .o-composer.active").toHaveCount(1);
expect(".o-speech-bubble").toHaveCount(0);
});

test("Can hover functions that require the cell position as context in grid composer", async () => {
setCellContent(model, "B2", "5");
await typeInComposerGrid("=ROW() + COLUMN() + B2");
await hoverComposerContent("ROW");
expect(".o-speech-bubble").toHaveText("1");
await hoverComposerContent("COLUMN");
expect(".o-speech-bubble").toHaveText("1");
await hoverComposerContent("=");
expect(".o-speech-bubble").toHaveText("7");
});

test("Hover is deactivated in a standalone composer", async () => {
const sheetId = model.getters.getActiveSheetId();
model.dispatch("ADD_CONDITIONAL_FORMAT", {
cf: {
rule: {
type: "CellIsRule",
operator: "Equal",
values: ["=ROW() + COLUMN() + B2"],
style: { fillColor: "#b6d7a8" },
},
id: "1",
},
ranges: [toRangeData(sheetId, "B1:D3")],
sheetId: sheetId,
});

env.openSidePanel("ConditionalFormatting", { selection: [toZone("B1")] });
await nextTick();

const composerSelector = ".o-sidePanel .o-composer";
await editStandaloneComposer(composerSelector, "=ROW() + COLUMN() + B2");
await click(fixture, composerSelector);

await hoverComposerContent("ROW");
expect(".o-speech-bubble").toHaveCount(0);
await hoverComposerContent("COLUMN");
expect(".o-speech-bubble").toHaveCount(0);
await hoverComposerContent("=");
expect(".o-speech-bubble").toHaveCount(0);
});
});
22 changes: 22 additions & 0 deletions tests/composer/composer_store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1086,6 +1086,28 @@ describe("edition", () => {
expect(getCellContent(model, cellOnLastCol)).toBe("0");
});

describe("Row addition works with position dependant functions", () => {
test("Adding rows below with ROW function", () => {
const sheetId = model.getters.getActiveSheetId();
const numberOfRows = model.getters.getNumberRows(sheetId);

const cellOnLastRow = toXC(0, numberOfRows - 1);
editCell(model, cellOnLastRow, "=MUNIT(ROW())");
expect(model.getters.getNumberRows(sheetId)).toBe(numberOfRows * 2 + 50 - 1);
expect(getCellContent(model, cellOnLastRow)).toBe("1");
});

test("Adding rows below with ROW function", () => {
const sheetId = model.getters.getActiveSheetId();
const numberOfCols = model.getters.getNumberCols(sheetId);

const cellOnLastCols = toXC(numberOfCols - 1, 0);
editCell(model, cellOnLastCols, "=MUNIT(COLUMN())");
expect(model.getters.getNumberCols(sheetId)).toBe(numberOfCols * 2 + 20 - 1);
expect(getCellContent(model, cellOnLastCols)).toBe("1");
});
});

test("Can undo/redo after adding a spreading formula at the end of the sheet", () => {
const sheetId = model.getters.getActiveSheetId();
const numberOfCols = model.getters.getNumberCols(sheetId);
Expand Down