Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Typst Language Support #289

Draft
wants to merge 16 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
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
190 changes: 190 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 31 additions & 1 deletion harper-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use ariadne::{Color, Label, Report, ReportKind, Source};
use clap::Parser;
use harper_comments::CommentParser;
use harper_core::linting::{LintGroup, LintGroupConfig, Linter};
use harper_core::parsers::Markdown;
use harper_core::parsers::{Markdown, Typst};
use harper_core::{remove_overlaps, Dictionary, Document, FstDictionary};

#[derive(Debug, Parser)]
Expand All @@ -26,6 +26,11 @@ enum Args {
/// The file you wish to parse.
file: PathBuf,
},
/// Parse a provided document and show the spans of the detected tokens.
Spans {
/// The file you wish to display the spans.
file: PathBuf,
},
/// Emit decompressed, line-separated list of words in Harper's dictionary.
Words,
}
Expand Down Expand Up @@ -84,6 +89,29 @@ fn main() -> anyhow::Result<()> {

Ok(())
}
Args::Spans { file } => {
let (doc, source) = load_file(&file)?;

let primary_color = Color::Blue;
let filename = file
.file_name()
.map(|s| s.to_string_lossy().into())
.unwrap_or("<file>".to_string());

let mut report_builder = Report::build(ReportKind::Advice, &filename, 0);
for token in doc.tokens() {
report_builder = report_builder.with_label(
Label::new((&filename, token.span.into()))
.with_message(format!("[{}, {})", token.span.start, token.span.end))
.with_color(primary_color),
);
}

let report = report_builder.finish();
report.print((&filename, Source::from(source)))?;

std::process::exit(1);
}
Args::Words => {
let dict = FstDictionary::curated();

Expand All @@ -107,6 +135,8 @@ fn load_file(file: &Path) -> anyhow::Result<(Document, String)> {
let mut parser: Box<dyn harper_core::parsers::Parser> =
if let Some("md") = file.extension().map(|v| v.to_str().unwrap()) {
Box::new(Markdown)
} else if let Some("typ") = file.extension().map(|v| v.to_str().unwrap()) {
Box::new(Typst)
} else {
Box::new(
CommentParser::new_from_filename(file)
Expand Down
1 change: 1 addition & 0 deletions harper-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ thiserror = "2.0.3"
unicode-blocks = "0.1.9"
unicode-width = "0.2.0"
levenshtein_automata = { version = "0.2.1", features = ["fst_automaton"] }
typst-syntax = "0.12.0"

[dev-dependencies]
criterion = { version = "0.5.1", default-features = false }
Expand Down
2 changes: 2 additions & 0 deletions harper-core/src/parsers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ mod isolate_english;
mod markdown;
mod mask;
mod plain_english;
mod typst;

use blanket::blanket;
pub use collapse_identifiers::CollapseIdentifiers;
pub use isolate_english::IsolateEnglish;
pub use markdown::Markdown;
pub use mask::Mask;
pub use plain_english::PlainEnglish;
pub use typst::Typst;

pub use crate::token::{Token, TokenKind, TokenStringExt};

Expand Down
Loading
Loading