Skip to content

Commit

Permalink
1.1.0
Browse files Browse the repository at this point in the history
Co-authored-by: mankool0 <8884398+mankool0@users.noreply.github.com>
LiamRiddell and mankool0 authored Jan 23, 2025

Partially verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
We cannot verify signatures from co-authors, and some of the co-authors attributed to this commit require their commits to be signed.
1 parent 2519178 commit c6dc790
Showing 9 changed files with 114 additions and 3,641 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "solve",
"name": "Solve",
"version": "1.0.3",
"version": "1.1.0",
"minAppVersion": "0.15.0",
"description": "Supercharge your notes with real-time calculations without AI fuss. From dates ('Now + 20 days'), percentages ('10% of 120'), units of measurement ('100cm + 2m'), arithmetic ('10 + 5') and more!",
"author": "Liam Riddell",
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "solve",
"version": "1.0.3",
"version": "1.1.0",
"description": "Supercharge your notes with real-time calculations without AI fuss. From dates ('Now + 20 days'), percentages ('10% of 120'), units of measurement ('100cm + 2m'), arithmetic ('10 + 5') and more!",
"main": "main.js",
"scripts": {
51 changes: 43 additions & 8 deletions src/codemirror/MarkdownEditorViewPlugin.ts
Original file line number Diff line number Diff line change
@@ -50,6 +50,14 @@ export class MarkdownEditorViewPlugin implements PluginValue {
IExpressionProcessorState,
string
>;
private expressionProcesserArray: StatefulPipeline<
IExpressionProcessorState,
string[]
>;
private expressionProcesserFinal: StatefulPipeline<
IExpressionProcessorState,
string
>;
private resultProcessor: StatefulPipeline<[IProvider, AnyResult], string>;
private variableProcessingStage: VariableProcessingStage;
private previousResultSubstitutionStage: PreviousResultSubstitutionStage;
@@ -72,7 +80,17 @@ export class MarkdownEditorViewPlugin implements PluginValue {
.addStage(SharedMarkdownRemovalStage)
.addStage(SharedCommentsRemovalStage)
.addStage(SharedMathJaxRemovalStage)
.addStage(SharedExtractInlineSolveStage)

this.expressionProcesserArray = new StatefulPipeline<
IExpressionProcessorState,
string[]
>()
.addStage(SharedExtractInlineSolveStage);

this.expressionProcesserFinal = new StatefulPipeline<
IExpressionProcessorState,
string
>()
.addStage(this.previousResultSubstitutionStage)
.addStage(this.variableProcessingStage)
.addStage(SharedExplicitModeRemovalStage)
@@ -183,7 +201,9 @@ export class MarkdownEditorViewPlugin implements PluginValue {

const line = view.state.doc.lineAt(linePosition);

let expression = line.text.trim();
let expression = line.text.trimStart();
let padding = line.text.length - expression.length;
expression = expression.trimEnd();

// Skip blank lines
if (!expression || expression.length === 0) {
@@ -216,17 +236,32 @@ export class MarkdownEditorViewPlugin implements PluginValue {
);
//logger.debug("After Expression Processor:", state, expression);

// The line is valid and decoration can be provided.
const decoration = this.provideDecoration(state, expression);
let inlineExpressions = this.expressionProcesserArray.process(
state,
[expression]
);

for (let i = 0; i < inlineExpressions.length; i++) {
const inlineExpression = inlineExpressions[i]
expression = this.expressionProcesserFinal.process(
state,
inlineExpression
);

// The line is valid and decoration can be provided.
const decoration = this.provideDecoration(state, expression);
if (!decoration) {
continue;
}

if (decoration) {
if (state.isInlineSolve && state.inlineSolveIndex) {
if (state.isInlineSolve && state.inlineSolveIndices) {
// Result is displayed at the end of the inline solve position
const inlineSolvePosition =
line.from + // Start of the line
3 + // Unaccounted inline solve characters s``
state.inlineSolveIndex + // Position of the inline solve
expression.length; // Length of the inline solve
state.inlineSolveIndices[i] + // Position of the inline solve
padding + // Length of removed whitespace
inlineExpression.length; // Length of the inline solve

builder.add(
inlineSolvePosition,
25 changes: 15 additions & 10 deletions src/pipelines/stages/expression/ExtractInlineSolveState.ts
Original file line number Diff line number Diff line change
@@ -3,23 +3,28 @@ import { IExpressionProcessorState } from "@/pipelines/stages/expression/state/I

export class ExtractInlineSolveStage extends BaseStatefulPipelineStage<
IExpressionProcessorState,
string
string[]
> {
// Matches for either s`EXPRESSION` and will return the first instance.
private inlineSolveRegex = new RegExp(/s`([^`]*)`/);
// Matches for either s`EXPRESSION`.
private inlineSolveRegex = new RegExp(/s`([^`]*)`/g);

protected execute(
state: IExpressionProcessorState,
request: string
): string {
request: string[]
): string[] {
// IMPORTANT: We match the raw input expression and not the processed expression because
// in cases where their is markdown elments e.g.lists, qouotes, etc...
const match = state.originalLineText.match(this.inlineSolveRegex);
// in cases where there are markdown elements e.g.lists, quotes, etc...
const matches: string[] = [];
const indices: number[] = [];
for (const match of state.originalLineText.matchAll(this.inlineSolveRegex)) {
matches.push(match[1]);
indices.push(match.index!);
}

if (match) {
if (matches.length > 0) {
state.isInlineSolve = true;
state.inlineSolveIndex = match.index;
return match[1];
state.inlineSolveIndices = indices;
return matches;
}

return request;
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@ export class PreviousResultSubstitutionStage extends BaseStatefulPipelineStage<
state: IExpressionProcessorState,
request: string
): string {
// If there is no previouds result then return the original request.
// If there is no previous result then return the original request.
if (this.previousResult === undefined) {
return request;
}
Original file line number Diff line number Diff line change
@@ -4,7 +4,8 @@ export interface IExpressionProcessorState {

// Inline solve support
isInlineSolve?: boolean;
inlineSolveIndex?: number;
inlineSolveIndices?: number[];
inlineMatches?: string[];

// Explicit solve support
isAllowedExplicitModeExpression?: boolean;
3,619 changes: 1 addition & 3,618 deletions styles.css

Large diffs are not rendered by default.

48 changes: 48 additions & 0 deletions test/pipeline/stages/ExtractInlineSolveStage.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { ExtractInlineSolveStage } from "@/pipelines/stages/expression/ExtractInlineSolveState";
import { IExpressionProcessorState } from "@/pipelines/stages/expression/state/IExpressionProcessorState";
import { beforeAll, describe, expect, test } from "@jest/globals";

let state: IExpressionProcessorState;
let stage: ExtractInlineSolveStage;

beforeAll(() => {
state = {
lineNumber: 0,
originalLineText: "",
};
stage = new ExtractInlineSolveStage();
});

describe("Inline Solve", () => {
test("Single Inline", () => {
const request = "s`1 + 2`";
state.originalLineText = request;
const result = stage.process(state, [request]);
expect(result).toBeDefined();
expect(result).toEqual(["1 + 2"]);
});

test("Multiple Inline", () => {
const request = "s`1 + 2` & s`2 - 1`";
state.originalLineText = request;
const result = stage.process(state, [request]);
expect(result).toBeDefined();
expect(result).toEqual(["1 + 2", "2 - 1"]);
});

test("Single Variable", () => {
const request = "s`:var + 1`";
state.originalLineText = request;
const result = stage.process(state, [request]);
expect(result).toBeDefined();
expect(result).toEqual([":var + 1"]);
});

test("Multiple Variables", () => {
const request = "s`:var + 1` & s`:var2 - 1`";
state.originalLineText = request;
const result = stage.process(state, [request]);
expect(result).toBeDefined();
expect(result).toEqual([":var + 1", ":var2 - 1"]);
});
});
3 changes: 2 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
@@ -16,5 +16,6 @@
"1.0.0": "0.15.0",
"1.0.1": "0.15.0",
"1.0.2": "0.15.0",
"1.0.3": "0.15.0"
"1.0.3": "0.15.0",
"1.1.0": "0.15.0"
}

0 comments on commit c6dc790

Please sign in to comment.