Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 3 additions & 3 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion texpand-cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "texpand-cli"
version = "0.1.1"
version = "0.1.2"
edition = "2024"

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion texpand-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "texpand-core"
version = "0.1.1"
version = "0.1.2"
edition = "2024"

[dependencies]
Expand Down
29 changes: 26 additions & 3 deletions texpand-core/src/compressor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,18 @@ impl CompressorState {
}

/// Emit a single token, applying identifier-spacing and `#`-newline rules.
pub fn emit_token(&mut self, text: &str) {
///
/// Set `skip_space_before` to `true` to suppress the identifier-space
/// insertion for the current token (used for `literal_suffix` inside a
/// `user_defined_literal` node, where `123_km` must remain `123_km`).
pub fn emit_token(&mut self, text: &str, skip_space_before: bool) {
if let Some(ch) = text.chars().next() {
// Any `#` leaf must sit on its own line.
if ch == '#' && !self.output.is_empty() && !self.output.ends_with('\n') {
self.output.push('\n');
}
if let Some(prev) = self.prev_last
if !skip_space_before
&& let Some(prev) = self.prev_last
&& is_ident_char(prev)
&& is_ident_char(ch)
{
Expand Down Expand Up @@ -208,7 +213,7 @@ fn compress_impl(
&& let Ok(text) = node.utf8_text(source.as_bytes())
&& !text.is_empty()
{
st.emit_token(text);
st.emit_token(text, node.kind() == "literal_suffix");
// `#define FOO …` — ensure at least one space between the
// macro name and the replacement text so that
// #define FOO"abc" and #define FOO(expr)
Expand Down Expand Up @@ -525,4 +530,22 @@ func();
);
assert!(s.contains("FOO(x)"), "function-like FOO(x): {s:?}");
}

#[test]
fn test_user_defined_literal_no_space() {
// User-defined literals must not get a space between the literal
// and its suffix: `123_km` not `123 _km`.
let s = compress_source("auto x = 123_km;\n");
assert!(s.contains("123_km"), "user-defined literal broken: {s:?}");
let s = compress_source("auto y = 1.5_deg;\n");
assert!(
s.contains("1.5_deg"),
"user-defined float literal broken: {s:?}"
);
let s = compress_source("auto y = 114min;\n");
assert!(
s.contains("114min"),
"literal without '_' prefix broken: {s:?}"
);
}
}
27 changes: 13 additions & 14 deletions texpand-core/src/expander.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ fn expand_recursive(
if !state.completed.contains(&key) {
if let Some(cs) = cs.as_mut() {
if let Ok(text) = node.utf8_text(source.as_bytes()) {
cs.emit_token(text);
cs.emit_token(text, false);
}
} else {
output.push_str(&source[node_start..node_end]);
Expand Down Expand Up @@ -282,20 +282,19 @@ fn expand_recursive(

// ── Everything else (leaf → emit text) ────────────────────
_ => {
if node.child_count() == 0 {
if let Some(ref mut cs) = cs {
if node.kind() != "comment"
&& let Ok(text) = node.utf8_text(source.as_bytes())
{
cs.emit_token(text);
}
} else {
if node_start > byte_pos {
output.push_str(&source[byte_pos..node_start]);
}
output.push_str(&source[node_start..node_end]);
byte_pos = node_end;
if node.child_count() == 0
&& let Some(ref mut cs) = cs
{
if node.kind() != "comment"
&& let Ok(text) = node.utf8_text(source.as_bytes())
{
cs.emit_token(text, false);
}
if node_start > byte_pos {
output.push_str(&source[byte_pos..node_start]);
}
output.push_str(&source[node_start..node_end]);
byte_pos = node_end;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion texpand-vscode/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "texpand-vscode"
version = "0.1.0"
version = "0.1.2"
edition = "2024"

[dependencies]
Expand Down
4 changes: 4 additions & 0 deletions texpand-vscode/extension/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ None yet.

## Release Notes

### 0.1.2

- Fix: user-defined literals (e.g. `123_km`) no longer get a spurious space inserted during compression.

### 0.1.0

Initial release. Basic expansion, compression, clipboard and file output.
4 changes: 4 additions & 0 deletions texpand-vscode/extension/README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@

## 更新日志

### 0.1.2

- 修复:用户定义字面量(例如 `123_km`)在压缩时不再被错误插入空格。

### 0.1.0

初始发布,支持基础展开、压缩、剪贴板与文件输出。
Loading