Skip to content

Commit

Permalink
add token count to editor
Browse files Browse the repository at this point in the history
  • Loading branch information
kaikalii committed Dec 3, 2023
1 parent 6b78dd3 commit c4b077d
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 84 deletions.
6 changes: 5 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

Uiua is not yet stable.

<!-- This version is not yet released. If you are reading this on the website, then these changes are live here. -->

## 0.6.0 - 2023-12-??
This version is not yet released. If you are reading this on the website, then these changes are live here.
### Website
- Add a token count to the editor (in settings)

## 0.5.1 - 2023-12-02
### Interpreter
Expand Down
165 changes: 95 additions & 70 deletions site/src/editor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use leptos::{ev::keydown, *};
use leptos_router::{use_navigate, BrowserIntegration, History, LocationChange, NavigateOptions};
use uiua::{
format::{format_str, FormatConfig},
is_ident_char, Primitive, SysOp,
is_ident_char, lex, Primitive, SysOp, Token,
};
use wasm_bindgen::{JsCast, JsValue};
use web_sys::{
Expand Down Expand Up @@ -82,11 +82,23 @@ pub fn Editor<'a>(

let (example, set_example) = create_signal(0);
let (output, set_output) = create_signal(View::default());
let (token_count, set_token_count) = create_signal(0);

let code_text = move || code_text(&code_id());
let get_code_cursor = move || get_code_cursor_impl(&code_id());
let (copied_link, set_copied_link) = create_signal(false);
let (settings_open, set_settings_open) = create_signal(false);
let update_token_count = move |code: &str| {
set_token_count.set(
lex(code, None)
.0
.into_iter()
.filter(|tok| {
!matches!(&tok.value, Token::Spaces | Token::Newline | Token::Comment)
})
.count(),
)
};

// Initialize the state
let state = Rc::new(State {
Expand Down Expand Up @@ -125,6 +137,8 @@ pub fn Editor<'a>(
cursor = Cursor::Ignore;
}

update_token_count(&code_text);

// Format code
let input = if format {
if let Ok(formatted) = format_str(
Expand Down Expand Up @@ -282,7 +296,9 @@ pub fn Editor<'a>(
return;
}
if let Some((start, _)) = get_code_cursor() {
state().set_code(&code_text(), Cursor::Set(start, start));
let code = code_text();
update_token_count(&code);
state().set_code(&code, Cursor::Set(start, start));
}
};

Expand Down Expand Up @@ -579,6 +595,7 @@ pub fn Editor<'a>(
if handled {
event.prevent_default();
event.stop_propagation();
update_token_count(&code_text());
}
});

Expand Down Expand Up @@ -966,72 +983,75 @@ pub fn Editor<'a>(
<div class="glyph-buttons">{glyph_buttons}</div>
</div>
<div id="settings" style=settings_style>
<div title="The maximum number of seconds a program can run for">
"Exec limit:"
<input
type="number"
min="0.01"
max="1000000"
width="3em"
value=get_execution_limit
on:input=on_execution_limit_change/>
"s"
</div>
<div title="The maximum number of seconds of audio &ast will generate">
<Prim prim=Primitive::Sys(SysOp::AudioStream) />" time:"
<input
type="number"
min="1"
max="600"
width="3em"
value=get_ast_time
on:input=on_ast_time_change/>
"s"
</div>
<div title="Place the cursor on the left of the current token when formatting">
"Format left:"
<input
type="checkbox"
checked=get_right_to_left
on:change=toggle_right_to_left/>
</div>
<div title="Automatically run pad links">
"Autorun links:"
<input
type="checkbox"
checked=get_autorun
on:change=toggle_autorun/>
</div>
<div>
"Stack:"
<select
on:change=on_select_top_at_top>
<option value="false" selected=get_top_at_top()>"Top at bottom"</option>
<option value="true" selected=get_top_at_top()>"Top at top"</option>
</select>
</div>
<div>
"Font size:"
<select
on:change=on_select_font_size>
<option value="0.6em" selected={get_font_size() == "0.6em"}>"Scalar"</option>
<option value="0.8em" selected={get_font_size() == "0.8em"}>"Small"</option>
<option value="1em" selected={get_font_size() == "1em"}>"Normal"</option>
<option value="1.2em" selected={get_font_size() == "1.2em"}>"Big"</option>
<option value="1.4em" selected={get_font_size() == "1.4em"}>"Rank 3"</option>
</select>
</div>
<div>
"Font:"
<select
on:change=on_select_font>
<option value="DejaVuSansMono" selected={get_font_name() == "DejaVuSansMono"}>"DejaVu"</option>
<option value="Uiua386" selected={get_font_name() == "Uiua386"}>"Uiua386"</option>
</select>
<div id="settings-left">
<div title="The maximum number of seconds a program can run for">
"Exec limit:"
<input
type="number"
min="0.01"
max="1000000"
width="3em"
value=get_execution_limit
on:input=on_execution_limit_change/>
"s"
</div>
<div title="The maximum number of seconds of audio &ast will generate">
<Prim prim=Primitive::Sys(SysOp::AudioStream) />" time:"
<input
type="number"
min="1"
max="600"
width="3em"
value=get_ast_time
on:input=on_ast_time_change/>
"s"
</div>
<div title="Place the cursor on the left of the current token when formatting">
"Format left:"
<input
type="checkbox"
checked=get_right_to_left
on:change=toggle_right_to_left/>
</div>
<div title="Automatically run pad links">
"Autorun links:"
<input
type="checkbox"
checked=get_autorun
on:change=toggle_autorun/>
</div>
<div>
"Stack:"
<select
on:change=on_select_top_at_top>
<option value="false" selected=get_top_at_top()>"Top at bottom"</option>
<option value="true" selected=get_top_at_top()>"Top at top"</option>
</select>
</div>
<div>
"Font size:"
<select
on:change=on_select_font_size>
<option value="0.6em" selected={get_font_size() == "0.6em"}>"Scalar"</option>
<option value="0.8em" selected={get_font_size() == "0.8em"}>"Small"</option>
<option value="1em" selected={get_font_size() == "1em"}>"Normal"</option>
<option value="1.2em" selected={get_font_size() == "1.2em"}>"Big"</option>
<option value="1.4em" selected={get_font_size() == "1.4em"}>"Rank 3"</option>
</select>
</div>
<div>
"Font:"
<select
on:change=on_select_font>
<option value="DejaVuSansMono" selected={get_font_name() == "DejaVuSansMono"}>"DejaVu"</option>
<option value="Uiua386" selected={get_font_name() == "Uiua386"}>"Uiua386"</option>
</select>
</div>
</div>
<button
class="info-button"
data-title=" shift Enter - Run + Format
<div id="settings-right">
<button
class="info-button"
data-title=" shift Enter - Run + Format
ctrl/⌘ / - Toggle line comment
ctrl/⌘ 4 - Toggle multiline string
alt Up/Down - Swap lines
Expand All @@ -1040,9 +1060,14 @@ ctrl/⌘ Z - Undo
ctrl/⌘ Y - Redo
replace \"pad\" in links with \"embed\"
or \"embedpad\" to embed the editor"
disabled>
"🛈"
</button>
disabled>
"🛈"
</button>
<div style="margin-right: 0.1em">
"Tokens: "
{ move || token_count.get() }
</div>
</div>
</div>
<div class=editor_class>
<div id="code-area">
Expand Down
36 changes: 26 additions & 10 deletions site/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ li {
.code,
.input-div,
input[type=text],
#settings>*>input,
#settings>*>select {
#settings>*>*>input,
#settings>*>*>select {
color: #d1daec;
background-color: #1d2c3a;
}
Expand Down Expand Up @@ -192,8 +192,8 @@ li {
.code,
.input-div,
input[type=text],
#settings>*>input,
#settings>*>select {
#settings>*>*>input,
#settings>*>*>select {
color: #344;
background-color: #f4f6f6;
}
Expand Down Expand Up @@ -354,15 +354,31 @@ li {
}

#settings {
font-size: 0.82em;
display: flex;
justify-content: space-between;
align-items: center;
flex-wrap: wrap;
font-size: 0.82em;
padding: 0.2em;
gap: 0.5em;
}

#settings>* {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 0.2em;
}

#settings-left {
width: 85%;
justify-content: space-between;
}

#settings-right {
flex-direction: column;
align-items: flex-end;
}

#settings>*>* {
display: inline-block;
white-space: nowrap;
}
Expand All @@ -372,14 +388,14 @@ input[type=number] {
-moz-appearance: textfield;
}

#settings>*>input,
#settings>*>select {
#settings>*>*>input,
#settings>*>*>select {
border-radius: 0.5em;
border: none;
margin-left: 0.5em;
}

#settings>*>input[type=number] {
#settings>*>*>input[type=number] {
width: 3em;
}

Expand Down
3 changes: 2 additions & 1 deletion src/lex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ use unicode_segmentation::UnicodeSegmentation;

use crate::{Primitive, UiuaError};

pub(crate) fn lex(input: &str, file: Option<&Path>) -> (Vec<Sp<Token>>, Vec<Sp<LexError>>) {
/// Lex a Uiua source file
pub fn lex(input: &str, file: Option<&Path>) -> (Vec<Sp<Token>>, Vec<Sp<LexError>>) {
Lexer {
input_segments: input.graphemes(true).collect(),
loc: Loc {
Expand Down
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,9 @@ fn run() -> UiuaResult {
},
Err(e)
if e.kind() == ErrorKind::InvalidSubcommand
&& env::args().nth(1).is_some_and(|file| file.ends_with(".ua")) =>
&& env::args()
.nth(1)
.is_some_and(|path| Path::new(&path).exists()) =>
{
let mut args: Vec<String> = env::args().collect();
args[0] = "run".into();
Expand Down
1 change: 0 additions & 1 deletion todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
Within each section, items are ordered (roughly) by decreasing priority.

## Features
- New `?` function
- Multimedia
- Sound input
- Webcam input
Expand Down

0 comments on commit c4b077d

Please sign in to comment.