Skip to content

Commit

Permalink
Review
Browse files Browse the repository at this point in the history
  • Loading branch information
farmaazon committed Mar 6, 2025
1 parent 5f90d2e commit 044c695
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 21 deletions.
12 changes: 1 addition & 11 deletions app/gui/src/project-view/components/ComponentBrowser.vue
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ function applyComponent(component: Opt<Component> = null) {
input.switchToCodeEditMode()
return Ok()
}
if (component.suggestionId) {
if (component.suggestionId != null) {
return input.applySuggestion(component.suggestionId)
} else {
// Component without suggestion database entry, for example "literal" component.
Expand All @@ -297,16 +297,6 @@ function acceptComponent(component: Opt<Component> = null) {
else result.error.log('Cannot apply suggestion')
}
// function editComponent(component: Opt<Component> = null) {
// const result = applyComponentToInput(component)
// if (result.ok) acceptInput()
// else result.error.log('Cannot apply suggestion')
// const suggestionId = component?.suggestionId ?? selectedSuggestionId.value
// if (suggestionId == null) return input.switchToCodeEditMode()
// const result = input.applySuggestion(suggestionId)
// if (!result.ok) result.error.log('Cannot apply suggestion')
// }
function acceptInput() {
const appliedReturnType =
input.mode.mode === 'codeEditing' ?
Expand Down
8 changes: 4 additions & 4 deletions app/gui/src/project-view/components/ComponentBrowser/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export type ComponentBrowserMode =
| {
mode: 'componentBrowsing'
filter: Filter
literal?: Ast.TextLiteral | Ast.NumericLiteral
literal?: Ast.TextLiteral | Ast.NumericLiteral | Ast.NegationApp | undefined
}
| {
mode: 'codeEditing'
Expand Down Expand Up @@ -111,10 +111,10 @@ export function useComponentBrowserInput(
: {}),
}
} else {
let literal: Ast.MutableTextLiteral | Ast.NumericLiteral | undefined =
let literal: Ast.MutableTextLiteral | Ast.NumericLiteral | Ast.NegationApp | undefined =
Ast.TextLiteral.tryParse(text.value)
if (literal == null) {
literal = Ast.NumericLiteral.tryParse(text.value)
literal = Ast.NumericLiteral.tryParseWithSign(text.value)
} else {
literal.fixBoundaries()
}
Expand All @@ -124,7 +124,7 @@ export function useComponentBrowserInput(
pattern: text.value,
...(sourceNodeType.value != null ? { selfArg: sourceNodeType.value } : {}),
},
...(literal ? { literal } : {}),
literal,
}
}
})
Expand Down
14 changes: 14 additions & 0 deletions app/gui/src/project-view/util/ast/__tests__/abstract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1092,6 +1092,20 @@ test('setRawTextContent promotes single-line uninterpolated text to interpolated
expect(literal.code()).toBe(`'${escapeTextLiteral(rawText)}'`)
})

test.each`
text | fixed
${"'abc'"} | ${"'abc'"}
${'"abc"'} | ${'"abc"'}
${"'''abc\n cde"} | ${"'''abc\n cde"}
${"'abc"} | ${"'abc'"}
${'"abc'} | ${'"abc"'}
`('Fixing boundaries in $text', ({ text, fixed }) => {
const literal = Ast.TextLiteral.tryParse(text)
assert(literal != null)
literal.fixBoundaries()
expect(literal.code()).toBe(fixed)
})

test.each([
{ code: 'operator1', expected: { subject: 'operator1', accesses: [] } },
{ code: 'operator1 foo bar', expected: { subject: 'operator1 foo bar', accesses: [] } },
Expand Down
31 changes: 29 additions & 2 deletions app/rust-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ pub fn parse_block(code: &str) -> Vec<u8> {
enso_parser::format::serialize(&ast).expect("Failed to serialize AST to binary format")
}

#[wasm_bindgen]
pub fn is_ident_or_operator(code: &str) -> u32 {
fn starts_with_ident_or_operator<const ONLY_ONE_TOKEN_ALLOWED: bool>(code: &str) -> u32 {
let parsed = enso_parser::lexer::run(code);
if parsed.internal_error.is_some() {
return 0;
}
let token = match &parsed.value[..] {
[token] => token,
[token, ..] if !ONLY_ONE_TOKEN_ALLOWED => token,
_ => return 0,
};
match &token.variant {
Expand All @@ -43,6 +43,17 @@ pub fn is_ident_or_operator(code: &str) -> u32 {
}
}

#[wasm_bindgen]
pub fn is_ident_or_operator(code: &str) -> u32 {
starts_with_ident_or_operator::<true>(code)
}

#[wasm_bindgen]
pub fn is_first_token_ident_or_operator(code: &str) -> u32 {
starts_with_ident_or_operator::<false>(code)
}


#[wasm_bindgen]
pub fn is_numeric_literal(code: &str) -> bool {
let parsed = PARSER.with(|parser| parser.parse_block(code));
Expand Down Expand Up @@ -83,4 +94,20 @@ mod tests {
assert!(!is_numeric_literal("1234!"));
assert!(!is_numeric_literal("1234e5"));
}

#[test]
fn test_checking_ident_or_operator() {
assert_eq!(is_ident_or_operator("abc"), 1);
assert_eq!(is_ident_or_operator("Abc"), 1);
assert_eq!(is_ident_or_operator("abc 14"), 0);
assert_eq!(is_ident_or_operator("+"), 2);
assert_eq!(is_ident_or_operator("+ 2"), 0);
assert_eq!(is_ident_or_operator("[]"), 0);

assert_eq!(is_first_token_ident_or_operator("abc"), 1);
assert_eq!(is_first_token_ident_or_operator("abc 14"), 1);
assert_eq!(is_first_token_ident_or_operator("+"), 2);
assert_eq!(is_first_token_ident_or_operator("+ 2"), 2);
assert_eq!(is_first_token_ident_or_operator("[]"), 0);
}
}
12 changes: 10 additions & 2 deletions app/ydoc-shared/src/ast/ffi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { createXXHash128 } from 'hash-wasm'
import type { IDataType } from 'hash-wasm/dist/lib/util'
import {
is_first_token_ident_or_operator,
is_ident_or_operator,
is_numeric_literal,
parse_block,
Expand All @@ -21,5 +22,12 @@ export function xxHash128(input: IDataType) {
return xxHasher128.digest()
}

/* eslint-disable-next-line camelcase */
export { is_ident_or_operator, is_numeric_literal, parse_block, parse_doc_to_json, parse_module }
/* eslint-disable camelcase */
export {
is_first_token_ident_or_operator,
is_ident_or_operator,
is_numeric_literal,
parse_block,
parse_doc_to_json,
parse_module,
}
2 changes: 2 additions & 0 deletions app/ydoc-shared/src/ast/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ export function isIdentifier(code: string): code is Identifier {
return is_ident_or_operator(code) === 1
}

export function startsWithIdentifier

/**
* Whether the given code is a type or constructor identifier.
* This is true if the code is an identifier beginning with an uppercase letter.
Expand Down
8 changes: 6 additions & 2 deletions app/ydoc-shared/src/ast/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1890,13 +1890,17 @@ export class MutableTextLiteral extends TextLiteral implements MutableExpression

setBoundaries(code: string) {
this.fields.set('open', unspaced(Token.new(code)))
this.fields.set('close', unspaced(Token.new(code)))
if (code.length > 1) {
this.fields.set('close', undefined)
} else {
this.fields.set('close', unspaced(Token.new(code)))
}
}

fixBoundaries() {
const open = this.open
const close = this.close
if (open != null && close == null) {
if (open != null && close == null && open.code().length === 1) {
this.fields.set('close', unspaced(Token.new(open.code())))
} else if (open == null && close != null) {
this.fields.set('open', unspaced(Token.new(close.code())))
Expand Down

0 comments on commit 044c695

Please sign in to comment.