Skip to content

Commit

Permalink
0.6.0 (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
LiamRiddell authored Nov 12, 2023
1 parent 5a35812 commit f0cda2a
Show file tree
Hide file tree
Showing 31 changed files with 404 additions and 15 deletions.
4 changes: 2 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "solve",
"name": "Solve",
"version": "0.5.1",
"version": "0.6.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!",
"author": "Liam Riddell",
Expand All @@ -11,4 +11,4 @@
"GitHub Sponser": "https://github.com/sponsors/LiamRiddell"
},
"isDesktopOnly": false
}
}
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": "0.5.1",
"version": "0.6.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!",
"main": "main.js",
"scripts": {
Expand Down
54 changes: 47 additions & 7 deletions src/codemirror/MarkdownEditorViewPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ResultWidget } from "@/codemirror/widgets/ResultWidget";
import { pluginEventBus } from "@/eventbus/PluginEventBus";
import { IResult } from "@/results/definition/IResult";
import UserSettings from "@/settings/UserSettings";
import { logger } from "@/utilities/Logger";
// @ts-expect-error
import { syntaxTree } from "@codemirror/language";
Expand All @@ -19,6 +20,8 @@ import { solveProviderManager } from "../providers/ProviderManager";

export class MarkdownEditorViewPlugin implements PluginValue {
public decorations: DecorationSet;
private userSettings: UserSettings;

private ignoreNodeForMaskString = [
"Document",
"quote",
Expand All @@ -31,6 +34,9 @@ export class MarkdownEditorViewPlugin implements PluginValue {

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

this.userSettings = UserSettings.getInstance();

this.decorations = this.buildDecorations(view);
}

Expand Down Expand Up @@ -140,8 +146,8 @@ export class MarkdownEditorViewPlugin implements PluginValue {
continue;
}

// Ignored nodes e.g. block qoutes (>), lists (-), checked list ([ ] Test) will leave the
// markdown formatting at the front we need these to be removed.
// Ignored nodes e.g. block qoutes (>), lists (-), checked list ([ ]) will remove the
// markdown formatting at the start of the string.
lineText = lineText.replace(
/^(?:(?:[-+*>]|(?:\[\s\])|(?:\d+\.))\s)+/m,
""
Expand Down Expand Up @@ -192,15 +198,49 @@ export class MarkdownEditorViewPlugin implements PluginValue {
}

private provideDecoration(sentence: string) {
let isExplicitlyDefinedSentence = false;

// When explicit mode is enabled the sentence will end with = sign.
// This needs to be removed in order for grammars to match.
if (this.userSettings.engine.explicitMode) {
if (sentence.trimEnd().endsWith("=")) {
sentence = sentence.substring(0, sentence.length - 1).trimEnd();
isExplicitlyDefinedSentence = true;
} else {
return undefined;
}
}

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

if (result !== undefined) {
return Decoration.widget({
widget: new ResultWidget(result),
side: 1,
});
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();

if (sentenceLowercasedTrimmed === resultLowercaseTrimmed) {
return undefined;
}

// If we're in explicit mode, we should only show the result if it was defined explicitly `=`
if (
this.userSettings.engine.explicitMode &&
!isExplicitlyDefinedSentence
) {
return undefined;
}

return Decoration.widget({
widget: new ResultWidget(result),
side: 1,
});
}

//#region Variables
Expand Down
1 change: 1 addition & 0 deletions src/providers/IProvider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export interface IProvider {
name: string;

enabled(): boolean;
provide<T = string>(sentence: string, raw: boolean): T | undefined;
}
4 changes: 4 additions & 0 deletions src/providers/ProviderBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export class ProviderBase implements IProvider {
this.formatVisitor = new FormatVisitor();
}

public enabled(): boolean {
throw new Error("Method not implemented.");
}

public provide<T = string>(sentence: string, raw: boolean): T | undefined {
throw new Error("Method not implemented.");
}
Expand Down
9 changes: 7 additions & 2 deletions src/providers/ProviderManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import { fastHash } from "@/utilities/FastHash";
import { logger } from "@/utilities/Logger";

class ProviderManager {
private providersMap: Map<number, IProvider>;
private _debugMode = false;
private providersMap: Map<number, IProvider>;
private settings: UserSettings;

constructor() {
Expand All @@ -30,6 +30,11 @@ class ProviderManager {
): string | undefined {
for (const [, provider] of this.providersMap) {
try {
// Skip providers that are not enabled
if (!provider.enabled()) {
continue;
}

let result = provider.provide(sentence, raw);

if (result !== undefined) {
Expand All @@ -38,8 +43,8 @@ class ProviderManager {
}

if (
!this.settings.engine.explicitMode &&
this.settings.arithmeticProvider
.renderEqualsBeforeResult
) {
result = `= ${result}`;
}
Expand Down
5 changes: 5 additions & 0 deletions src/providers/arithmetic/BasicArithmeticProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import grammar, {
} from "@/grammars/arithmetic/BasicArithmetic.ohm-bundle";
import { SemanticProviderBase } from "@/providers/SemanticProviderBase";
import { basicArithmeticSemanticActions } from "@/providers/arithmetic/ArithmeticSemantics";
import UserSettings from "@/settings/UserSettings";
import { logger } from "@/utilities/Logger";

export class BasicArithmeticProvider extends SemanticProviderBase<BasicArithmeticSemantics> {
Expand All @@ -14,6 +15,10 @@ export class BasicArithmeticProvider extends SemanticProviderBase<BasicArithmeti
this.semantics.addOperation("visit()", basicArithmeticSemanticActions);
}

enabled() {
return UserSettings.getInstance().arithmeticProvider.enabled;
}

provide<T = string>(sentence: string, raw: boolean = true): T | undefined {
try {
const matchResult = grammar.match(sentence);
Expand Down
4 changes: 4 additions & 0 deletions src/providers/datetime/DatetimeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ export class DatetimeProvider extends SemanticProviderBase<DatetimeSemantics> {
);
}

enabled() {
return UserSettings.getInstance().datetimeProvider.enabled;
}

provide<T = string>(sentence: string, raw: boolean = true): T | undefined {
try {
const matchResult = grammar.match(sentence);
Expand Down
5 changes: 5 additions & 0 deletions src/providers/function/FunctionArithmeticProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { SemanticProviderBase } from "@/providers/SemanticProviderBase";
import { basicArithmeticSemanticActions } from "@/providers/arithmetic/ArithmeticSemantics";
import { HexResult } from "@/results/HexResult";
import { NumberResult } from "@/results/NumberResult";
import UserSettings from "@/settings/UserSettings";
import { logger } from "@/utilities/Logger";

export class FunctionArithmeticProvider extends SemanticProviderBase<FunctionArithmeticSemantics> {
Expand Down Expand Up @@ -50,6 +51,10 @@ export class FunctionArithmeticProvider extends SemanticProviderBase<FunctionAri
});
}

enabled() {
return UserSettings.getInstance().functionArithmeticProvider.enabled;
}

provide<T = string>(sentence: string, raw: boolean = true): T | undefined {
try {
const matchResult = grammar.FunctionArithmetic.match(sentence);
Expand Down
5 changes: 5 additions & 0 deletions src/providers/percentage/PercentageArithmeticProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { basicArithmeticSemanticActions } from "@/providers/arithmetic/Arithmeti
import { HexResult } from "@/results/HexResult";
import { NumberResult } from "@/results/NumberResult";
import { PercentageResult } from "@/results/PercentageResult";
import UserSettings from "@/settings/UserSettings";
import { logger } from "@/utilities/Logger";
import { DecreaseByVisitor } from "@/visitors/percentage/DecreaseByVisitor";
import { IncreaseByVisitor } from "@/visitors/percentage/IncreaseByVisitor";
Expand Down Expand Up @@ -56,6 +57,10 @@ export class PercentageArithmeticProvider extends SemanticProviderBase<Percentag
});
}

enabled() {
return UserSettings.getInstance().percentageArithmeticProvider.enabled;
}

provide<T = string>(sentence: string, raw: boolean = true): T | undefined {
try {
const matchResult = grammar.PercentageArithmetic.match(sentence);
Expand Down
5 changes: 5 additions & 0 deletions src/providers/uom/UnitsOfMeasurementProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { PercentageResult } from "@/results/PercentageResult";
import { StringResult } from "@/results/StringResult";
import { UnitOfMeasurementResult } from "@/results/UnitOfMeasurementResult";
import { INumericResult } from "@/results/definition/INumericResult";
import UserSettings from "@/settings/UserSettings";
import { logger } from "@/utilities/Logger";
import convert, { Unit } from "convert-units";

Expand Down Expand Up @@ -73,6 +74,10 @@ export class UnitsOfMeasurementProvider extends SemanticProviderBase<UnitsOfMeas
});
}

enabled() {
return UserSettings.getInstance().unitOfMeasurementProvider.enabled;
}

provide<T = string>(sentence: string, raw: boolean = true): T | undefined {
try {
const matchResult =
Expand Down
5 changes: 5 additions & 0 deletions src/providers/vector/VectorArithmeticProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { NumberResult } from "@/results/NumberResult";
import { Vector2Result } from "@/results/Vector2Result";
import { Vector3Result } from "@/results/Vector3Result";
import { Vector4Result } from "@/results/Vector4Result";
import UserSettings from "@/settings/UserSettings";
import { logger } from "@/utilities/Logger";
import { Vector2 } from "@/utilities/Vector2";
import { Vector3 } from "@/utilities/Vector3";
Expand Down Expand Up @@ -520,6 +521,10 @@ export class VectorArithmeticProvider extends ProviderBase {
return undefined;
}

enabled() {
return UserSettings.getInstance().vectorArithmeticProvider.enabled;
}

provide<T = string>(sentence: string, raw: boolean = true): T | undefined {
try {
const result = this.tryParseVectorArithmetic(sentence);
Expand Down
18 changes: 18 additions & 0 deletions src/settings/PluginSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,36 @@ import { IPluginSettings } from "@/settings/definition/IPluginSettings";
import moment from "moment";

export const DEFAULT_SETTINGS: IPluginSettings = {
engine: {
explicitMode: false,
},

interface: {
renderResultEndOfLine: false,
showStatusBarCompanion: true,
},

// Providers
arithmeticProvider: {
enabled: true,
renderEqualsBeforeResult: true,
},
functionArithmeticProvider: {
enabled: true,
},
vectorArithmeticProvider: {
enabled: true,
},
percentageArithmeticProvider: {
enabled: true,
},
datetimeProvider: {
enabled: true,
parsingFormat: EDatetimeParsingFormat.EU,
},
unitOfMeasurementProvider: {
enabled: true,
},

// Results
integerResult: {},
Expand Down
Loading

0 comments on commit f0cda2a

Please sign in to comment.