Skip to content

Commit

Permalink
Add setting to hide unnecessary delimiters #27
Browse files Browse the repository at this point in the history
  • Loading branch information
supersurviveur committed Jun 6, 2024
1 parent 977dabe commit 0a2708c
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 951 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ By default, the extension will use the monokai theme colors.
- **Colors**: Select your theme colors.
- **RenderSymbolsOutsideMath**: If set to true, the extension will render symbols everywhere in the document, not only in math equations.
- **RenderSpaces**: If set to true, the extension will render space symbols like space, wj, space.quad...
- **HideUnnecessaryDelimiters**: If set to true, the extension will hide unnecessary delimiters in math equations, like paretheses in `x^(2 x)`
- **RenderingMode**: Choose whether to render only simple symbols or also complex equations.
- **RevealOffset**: The number of lines to reveal before and after the current line.
- **CustomSymbols**: You can add or override symbols with your own. The format is
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@
"default": false,
"markdownDescription": "If true, space symbols like space, space.quad, wjoin... will be rendered."
},
"typst-math.hideUnnecessaryDelimiters": {
"type": "boolean",
"default": false,
"markdownDescription": "If true, unnecessary delimiters like parentheses in `$x^(2 x y)$` will be hidden."
},
"typst-math.revealOffset": {
"type": "number",
"default": 0,
Expand Down
6 changes: 4 additions & 2 deletions src/decorations/decorations.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as vscode from 'vscode';
import { createDecorationType, strictIntersection } from './helpers';
import { Logger } from '../logger';
import { blacklistedSymbols, getColors, getRenderingMode, customSymbols, renderSpaces, renderSymbolsOutsideMath, revealOffset } from '../utils';
import { blacklistedSymbols, getColors, getRenderingMode, customSymbols, renderSpaces, renderSymbolsOutsideMath, revealOffset, hideUnnecessaryDelimiters } from '../utils';
import getWASM from '../wasmHelper';
import { updateStatusBarItem } from '../statusbar';
import { CustomSymbol } from 'typst-math-rust';
Expand Down Expand Up @@ -32,6 +32,7 @@ export class Decorations {
renderingMode = getRenderingMode();
renderOutsideMath = renderSymbolsOutsideMath();
renderSpaces = renderSpaces();
hideUnnecessaryDelimiters = hideUnnecessaryDelimiters();
blacklistedSymbols = blacklistedSymbols();
reveal_offset = revealOffset();
customSymbols: CustomSymbol[] = [];
Expand Down Expand Up @@ -103,6 +104,7 @@ export class Decorations {
this.renderingMode = getRenderingMode();
this.renderOutsideMath = renderSymbolsOutsideMath();
this.renderSpaces = renderSpaces();
this.hideUnnecessaryDelimiters = hideUnnecessaryDelimiters();
this.blacklistedSymbols = blacklistedSymbols();
this.reveal_offset = revealOffset();
this.clearDecorations();
Expand All @@ -119,7 +121,7 @@ export class Decorations {

let start = this.edition_state.edited_range?.start.line === undefined ? -1 : this.edition_state.edited_range?.start.line;
let end = this.edition_state.edited_range?.end.line === undefined ? -1 : this.edition_state.edited_range?.end.line;
let parsed = getWASM().parse_document(this.activeEditor.document.getText() as string, start, end, this.renderingMode, this.renderOutsideMath, this.renderSpaces, this.blacklistedSymbols, this.customSymbols);
let parsed = getWASM().parse_document(this.activeEditor.document.getText() as string, start, end, this.renderingMode, this.renderOutsideMath, this.renderSpaces, this.hideUnnecessaryDelimiters, this.blacklistedSymbols, this.customSymbols);

// If edited lines aren't defined, we clear all ranges
// If they are defined, remove symbols whiwh were rendered again, and trnaslate ones after the edition
Expand Down
4 changes: 4 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ export function renderSymbolsOutsideMath() {
export function renderSpaces() {
return config.get<boolean>('renderSpaces') || false;
}
// Retreive the settings for delimiter rendering
export function hideUnnecessaryDelimiters() {
return config.get<boolean>('hideUnnecessaryDelimiters') || false;
}
// Retreive the settings for space rendering
export function revealOffset() {
return config.get<number>('revealOffset') || 0;
Expand Down
2 changes: 2 additions & 0 deletions typst-math-rust/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub struct Options {
pub rendering_mode: u8,
pub render_outside_math: bool,
pub render_spaces: bool,
pub hide_unnecessary_delimiters: bool,
pub blacklisted_symbols: Vec<String>,
pub custom_symbols: HashMap<String, CustomSymbol>,
}
Expand All @@ -41,6 +42,7 @@ impl Default for Options {
rendering_mode: 3,
render_outside_math: true,
render_spaces: false,
hide_unnecessary_delimiters: false,
blacklisted_symbols: vec![],
custom_symbols: HashMap::new(),
}
Expand Down
5 changes: 4 additions & 1 deletion typst-math-rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,13 @@ pub fn parse_document(
rendering_mode: u8,
render_outside_math: bool,
render_spaces: bool,
hide_unnecessary_delimiters: bool,
blacklisted_symbols: Vec<String>,
custom_symbols: Vec<CustomSymbol>,
) -> Parsed {
// Generate a fake source
let mut source = typst_syntax::Source::detached(content.to_string());
// println!("{:#?}", source.root());
println!("{:#?}", source.root());

// These variable contains the range of the document that was parsed incrementally and will be returned to the extension
let mut edit_start_line = 0;
Expand Down Expand Up @@ -118,6 +119,7 @@ pub fn parse_document(
rendering_mode,
render_outside_math,
render_spaces,
hide_unnecessary_delimiters,
blacklisted_symbols,
custom_symbols,
};
Expand Down Expand Up @@ -170,6 +172,7 @@ mod tests {
3,
true,
true,
false,
vec![],
vec![generate_custom_symbol(
"symbol".to_string(),
Expand Down
Loading

0 comments on commit 0a2708c

Please sign in to comment.