Skip to content

Commit

Permalink
0.9.0 (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
LiamRiddell authored Dec 11, 2023
1 parent 388b500 commit fd1ac68
Show file tree
Hide file tree
Showing 22 changed files with 404 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"prettier.configPath": "",
"editor.codeActionsOnSave": {
"source.organizeImports": true
"source.organizeImports": "explicit"
},
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnPaste": true,
Expand Down
6 changes: 3 additions & 3 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"id": "solve",
"name": "Solve",
"version": "0.8.2",
"version": "0.9.0",
"minAppVersion": "0.15.0",
"description": "Level Up Your Notes: Introducing Solve – Your Math Maestro! Real-time calculations without AI fuss. From date magic ('Now + 20 days') to arithmetic flair ('10 + 5'), your trusted sidekick in every note. More brilliance on the way!",
"description": "Supercharge your notes in Obsdian with real-time calculations without AI fuss. From date magic ('Now + 20 days') to arithmetic flair ('10 + 5').",
"author": "Liam Riddell",
"authorUrl": "https://github.com/LiamRiddell",
"fundingUrl": {
"Buy Me a Coffee": "https://www.buymeacoffee.com/liamriddell",
"GitHub Sponser": "https://github.com/sponsors/LiamRiddell"
},
"isDesktopOnly": false
}
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "solve",
"version": "0.8.2",
"description": "Supercharge your notes in with real-time calculations without AI fuss. From date magic ('Now + 20 days') to arithmetic flair ('10 + 5').",
"version": "0.9.0",
"description": "Supercharge your notes in Obsdian with real-time calculations without AI fuss. From date magic ('Now + 20 days') to arithmetic flair ('10 + 5').",
"main": "main.js",
"scripts": {
"dev": "node esbuild.config.mjs",
Expand Down
23 changes: 17 additions & 6 deletions src/codemirror/MarkdownEditorViewPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { pluginEventBus } from "@/eventbus/PluginEventBus";
import { Pipeline } from "@/pipelines/definition/Pipeline";
import { SharedCommentsRemovalStage } from "@/pipelines/stages/CommentsRemovalStage";
import { SharedMarkdownRemovalStage } from "@/pipelines/stages/MarkdownRemovalStage";
import { PreviousResultSubstitutionStage } from "@/pipelines/stages/PreviousResultSubstitutionStage";
import { VariableProcessingStage } from "@/pipelines/stages/VariableProcessingStage";
import UserSettings from "@/settings/UserSettings";
import { logger } from "@/utilities/Logger";
Expand Down Expand Up @@ -33,15 +34,20 @@ export class MarkdownEditorViewPlugin implements PluginValue {
];

private processingPipeline: Pipeline<string>;
private previousResultSubstitutionStage: PreviousResultSubstitutionStage;

constructor(view: EditorView) {
logger.debug(`[SolveViewPlugin] Constructer`);

this.userSettings = UserSettings.getInstance();

this.previousResultSubstitutionStage =
new PreviousResultSubstitutionStage();

this.processingPipeline = new Pipeline<string>()
.addStage(SharedMarkdownRemovalStage)
.addStage(SharedCommentsRemovalStage)
.addStage(this.previousResultSubstitutionStage)
.addStage(new VariableProcessingStage());

this.decorations = this.buildDecorations(view);
Expand Down Expand Up @@ -221,20 +227,20 @@ export class MarkdownEditorViewPlugin implements PluginValue {
}

// Initial implementation will show the first valid result from available providers.
const result = solveProviderManager.provideFirst(sentence);
const result = solveProviderManager.provideFirst<string>(sentence);

if (result === undefined) {
return undefined;
}

// If the input sentence and the output is the same value ignore it.
// For example, 10 = 10
const sentenceLowercasedTrimmed = sentence.toLowerCase().trim();
const resultLowercaseTrimmed = result.startsWith("= ")
? result.substring(2).toLocaleLowerCase().trim()
: result.toLowerCase().trim();
const sentenceTrimmed = sentence.trim();
const resultTrimmed = result.startsWith("= ")
? result.substring(2).trim()
: result.trim();

if (sentenceLowercasedTrimmed === resultLowercaseTrimmed) {
if (sentenceTrimmed.toLowerCase() === resultTrimmed.toLowerCase()) {
return undefined;
}

Expand All @@ -246,6 +252,11 @@ export class MarkdownEditorViewPlugin implements PluginValue {
return undefined;
}

// Updates the previous solve to be the new solve that's passed the checks
this.previousResultSubstitutionStage.setPreviousResultString(
resultTrimmed
);

return Decoration.widget({
widget: new ResultWidget(result, lineNumber),
side: 1,
Expand Down
2 changes: 1 addition & 1 deletion src/grammars/arithmetic/BasicArithmetic.ohm-bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/grammars/function/FunctionArithmetic.ohm-bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/grammars/percentage/PercentageArithmetic.ohm-bundle.js

Large diffs are not rendered by default.

28 changes: 15 additions & 13 deletions src/grammars/uom/UnitsOfMeasurement.ohm
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
include '../arithmetic/BasicArithmetic.ohm'

UnitsOfMeasurementArithmetic <: BasicArithmetic {
Expression
:= Conversion
Expression
:=
| Conversion
| ConversionPossiblities
| LogicalShift

Primitive
+= UoM
+= uom
| percentage

Conversion
= caseInsensitive<"convert">? UoM caseInsensitive<"to"> caseInsensitive<"best"> -- best
| caseInsensitive<"convert">? UoM caseInsensitive<"to"> Unit -- convert
= caseInsensitive<"convert">? LogicalShift caseInsensitive<"to"> caseInsensitive<"best"> -- best
| caseInsensitive<"convert">? LogicalShift caseInsensitive<"to"> unit -- convert

ConversionPossiblities
= caseInsensitive<"convert">? UoM caseInsensitive<"to"> "?"?
= caseInsensitive<"convert">? LogicalShift caseInsensitive<"to"> "?"?

UoM
= number Unit

Unit
percentage
= number "%"

// Important: The reason we include `~("to" space+)` is to prevent matching "t" or "torr" units when used in expression "convert 100g + 20 to g"
uom
= number spaces ~("to" ~"rr") spaces unit

unit
= "km3/s"
| "yd3/h"
| "yd3/min"
Expand Down Expand Up @@ -191,7 +196,4 @@ UnitsOfMeasurementArithmetic <: BasicArithmetic {
| "Gb"
| "Mb"
| "b"

percentage
= number "%"
}
4 changes: 2 additions & 2 deletions src/grammars/uom/UnitsOfMeasurement.ohm-bundle.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ export interface UnitsOfMeasurementArithmeticActionDict<T> extends BasicArithmet
Conversion_convert?: (this: NonterminalNode, arg0: IterationNode, arg1: NonterminalNode, arg2: NonterminalNode, arg3: NonterminalNode) => T;
Conversion?: (this: NonterminalNode, arg0: NonterminalNode) => T;
ConversionPossiblities?: (this: NonterminalNode, arg0: IterationNode, arg1: NonterminalNode, arg2: NonterminalNode, arg3: IterationNode) => T;
UoM?: (this: NonterminalNode, arg0: NonterminalNode, arg1: NonterminalNode) => T;
Unit?: (this: NonterminalNode, arg0: TerminalNode) => T;
percentage?: (this: NonterminalNode, arg0: NonterminalNode, arg1: TerminalNode) => T;
uom?: (this: NonterminalNode, arg0: NonterminalNode, arg1: NonterminalNode, arg2: NonterminalNode, arg3: NonterminalNode) => T;
unit?: (this: NonterminalNode, arg0: TerminalNode) => T;
}

export interface UnitsOfMeasurementArithmeticSemantics extends Semantics {
Expand Down
2 changes: 1 addition & 1 deletion src/grammars/uom/UnitsOfMeasurement.ohm-bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/grammars/vector/Vector2Arithmetic.ohm-bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/grammars/vector/Vector3Arithmetic.ohm-bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/grammars/vector/Vector4Arithmetic.ohm-bundle.js

Large diffs are not rendered by default.

37 changes: 37 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,43 @@ export default class SolvePlugin extends Plugin {
}
},
});

this.addCommand({
id: "commit-result-selection-visible",
name: "Commit results in selection",
editorCallback(editor, ctx) {
const selectionStart = editor.getCursor("from");
const selectionEnd = editor.getCursor("to");

if (
selectionStart.line === selectionEnd.line &&
selectionStart.ch === selectionEnd.ch
) {
new Notice("Solve: Failed to commit, no text selected.");
return;
}

const { containerEl } = editor as any;

if (!containerEl) {
return;
}

for (
let i = selectionStart.line + 1;
i < selectionEnd.line + 1;
i++
) {
const resultElement = (
containerEl as HTMLElement
).querySelector<HTMLElement>(`#osr-${i + 1}`);

if (resultElement) {
resultElement.click();
}
}
},
});
}

public async setStatusBarCompanionVisibility(visible: boolean) {
Expand Down
52 changes: 52 additions & 0 deletions src/pipelines/stages/PreviousResultSubstitutionStage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { BasePipelineStage } from "@/pipelines/definition/stages/BasePipelineStage";
import { IResult } from "@/results/definition/IResult";
import { ResultSubstitutionFormatVisitor } from "@/visitors/format/VariableSubstitutionFormatVisitor";

// Important: Since this stage is stateful it can not be used in a shared context.
export class PreviousResultSubstitutionStage extends BasePipelineStage<string> {
private previousResultSubstitutionRegex = new RegExp(/\$prev/gi);
private resultSubstitutionVisitor: ResultSubstitutionFormatVisitor;

// TODO: We want to switch to passing around IResult as opposed a string as this is more re-usable.
private previousResult: IResult<any>;
private previousResultString: string;

constructor() {
super();
this.resultSubstitutionVisitor = new ResultSubstitutionFormatVisitor();
}

protected execute(request: string): string {
// Substitute previous solve into the expression
// if (this.previousResult) {
// console.log(
// "PREVIOUS SOLVE",
// this.previousResult,
// "EXPRESSION",
// request
// );

// request = request.replace(
// this.previousResultSubstitutionRegex,
// this.resultSubstitutionVisitor.visit(this.previousResult)
// );
// }

if (this.previousResultString) {
return request.replace(
this.previousResultSubstitutionRegex,
this.previousResultString
);
}

return request;
}

public setPreviousResult(result: IResult<any>) {
this.previousResult = result;
}

public setPreviousResultString(string: string) {
this.previousResultString = string;
}
}
13 changes: 11 additions & 2 deletions src/pipelines/stages/VariableProcessingStage.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { BasePipelineStage } from "@/pipelines/definition/stages/BasePipelineStage";
import { solveProviderManager } from "@/providers/ProviderManager";
import { IResult } from "@/results/definition/IResult";
import { ResultSubstitutionFormatVisitor } from "@/visitors/format/VariableSubstitutionFormatVisitor";

// Important: Since this stage is stateful it can not be used in a shared context.
export class VariableProcessingStage extends BasePipelineStage<string> {
private variableAssignmentRegex = new RegExp(/^(\$\w+)\s+=/);
private variableSubstitutionRegex = new RegExp(/(\$\w+)/g);
private resultSubstitutionVisitor = new ResultSubstitutionFormatVisitor();

private variableMap = new Map<string, IResult<any>>();

protected execute(request: string): string {
Expand Down Expand Up @@ -43,14 +46,20 @@ export class VariableProcessingStage extends BasePipelineStage<string> {
);

if (result !== undefined) {
// Save the mapping to the variable name to result map table
this.variableMap.set(variableName, result as any as IResult<any>);
}
}

private substituteVariables(expression: string): string {
return expression.replace(this.variableSubstitutionRegex, (match) => {
const variableValue = this.variableMap.get(match)?.value;
return variableValue !== undefined ? variableValue : match;
const variableResult = this.variableMap.get(match);

if (typeof variableResult === "undefined") {
return match;
}

return this.resultSubstitutionVisitor.visit(variableResult);
});
}
}
14 changes: 8 additions & 6 deletions src/providers/ProviderManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ class ProviderManager {
this.providersMap.set(fastHash(provider.name), provider);
}

public provideFirst(
public provideFirst<T>(
sentence: string,
raw: boolean = false
): string | undefined {
): T | undefined {
for (const [, provider] of this.providersMap) {
try {
// Skip providers that are not enabled
Expand All @@ -40,7 +40,7 @@ class ProviderManager {

if (result !== undefined) {
if (raw) {
return result;
return result as T;
}

if (
Expand All @@ -50,9 +50,11 @@ class ProviderManager {
result = `= ${result}`;
}

return this._debugMode
? `${result} [${provider.name}]`
: `${result}`;
return (
this._debugMode
? `${result} [${provider.name}]`
: `${result}`
) as T;
}
} catch (error) {
logger.error(error);
Expand Down
4 changes: 2 additions & 2 deletions src/providers/uom/UnitsOfMeasurementProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ export class UnitsOfMeasurementProvider extends SemanticProviderBase<UnitsOfMeas

return new StringResult(possibilities);
},
UoM(numberNode, unitNode) {
uom(numberNode, _, _1, unitNode) {
const number = numberNode.visit() as INumericResult;
const unit = unitNode.visit() as UnitOfMeasurementResult;

return new UnitOfMeasurementResult(number.value, unit.unit);
},
Unit(_) {
unit(_) {
return new UnitOfMeasurementResult(0, this.sourceString);
},
percentage(numberNode, _) {
Expand Down
Loading

0 comments on commit fd1ac68

Please sign in to comment.