Skip to content

Commit

Permalink
derp
Browse files Browse the repository at this point in the history
  • Loading branch information
fonsp committed Aug 10, 2024
1 parent fe1428a commit 2478e62
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 44 deletions.
21 changes: 11 additions & 10 deletions frontend/components/CellInput/LiveDocsFromCursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { EditorState, syntaxTree } from "../../imports/CodemirrorPlutoSetup.js"
import { ScopeStateField } from "./scopestate_statefield.js"

let get_root_variable_from_expression = (cursor) => {
if (cursor.name === "SubscriptExpression") {
if (cursor.name === "IndexExpression") {
cursor.firstChild()
return get_root_variable_from_expression(cursor)
}
Expand All @@ -20,12 +20,13 @@ let get_root_variable_from_expression = (cursor) => {
let VALID_DOCS_TYPES = [
"Identifier",
"FieldExpression",
"SubscriptExpression",
"IndexExpression",
"MacroFieldExpression",
"MacroIdentifier",
"Operator",
"Definition",
"ParameterizedIdentifier",
"TypeHead",
"Signature",
"ParametrizedExpression",
]
let keywords_that_have_docs_and_are_cool = [
"import",
Expand All @@ -49,12 +50,12 @@ let is_docs_searchable = (/** @type {import("../../imports/CodemirrorPlutoSetup.
} else if (VALID_DOCS_TYPES.includes(cursor.name)) {
if (cursor.firstChild()) {
do {
// Numbers themselves can't be docs searched, but using numbers inside SubscriptExpression can be.
if (cursor.name === "Number") {
// Numbers themselves can't be docs searched, but using numbers inside IndexExpression can be.
if (cursor.name === "IntegerLiteral" || cursor.name === "FloatLiteral") {
continue
}
// This is for the VERY specific case like `Vector{Int}(1,2,3,4) which I want to yield `Vector{Int}`
if (cursor.name === "TypeArgumentList") {
if (cursor.name === "BraceExpression") {
continue
}
if (cursor.name === "FieldName" || cursor.name === "MacroName" || cursor.name === "MacroFieldName") {
Expand Down Expand Up @@ -129,7 +130,7 @@ export let get_selected_doc_from_state = (/** @type {EditorState} */ state, verb
// - Struct name is useless: you are looking at the definition
// - Properties are just named, not in the workspace or anything
// Only thing we do want, are types and the right hand side of `=`'s.
if (parents.includes("AssignmentExpression") && parents.indexOf("AssignmentExpression") < index_of_struct_in_parents) {
if (parents.includes("binding") && parents.indexOf("binding") < index_of_struct_in_parents) {
// We're inside a `... = ...` inside the struct
} else if (parents.includes("TypedExpression") && parents.indexOf("TypedExpression") < index_of_struct_in_parents) {
// We're inside a `x::X` inside the struct
Expand Down Expand Up @@ -229,7 +230,7 @@ export let get_selected_doc_from_state = (/** @type {EditorState} */ state, verb
}

// `a = 1` would yield `=`, `a += 1` would yield `+=`
if (cursor.name === "AssignmentExpression") {
if (cursor.name === "binding") {
let end_of_first = cursor.node.firstChild.to
let beginning_of_last = cursor.node.lastChild.from
return state.doc.sliceString(end_of_first, beginning_of_last).trim()
Expand Down Expand Up @@ -320,7 +321,7 @@ export let get_selected_doc_from_state = (/** @type {EditorState} */ state, verb
return undefined
}
// If we are expanding to an AssigmentExpression, we DONT want to show `=`
if (parent.name === "AssignmentExpression") {
if (parent.name === "binding") {
return undefined
}
} finally {
Expand Down
14 changes: 8 additions & 6 deletions frontend/components/CellInput/lezer_template.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ export let match_template = (haystack_cursor, template, matches, verbose = false
// Skip comments
// TODO This is, I think, one of the few julia-only things right now.....
// .... Any sane way to factor this out?
while (haystack_cursor.name === "Comment" || haystack_cursor.name === "BlockComment") {
while (haystack_cursor.name === "LineComment" || haystack_cursor.name === "BlockComment") {
if (!haystack_cursor.nextSibling()) break
}

Expand Down Expand Up @@ -466,7 +466,11 @@ export let to_template = function* (julia_code_object) {
}
})
if (unused_substitutions.length > 0) {
throw new Error(`Some substitutions not applied, this means it couldn't be matched to a AST position: ${JSON.stringify(unused_substitutions)}`)
throw new Error(
`Some substitutions not applied, this means it couldn't be matched to a AST position.\n\nUnused substitutions: ${JSON.stringify(
unused_substitutions
)}\n`
)
}
return result
} else if (typeof julia_code_object === "function") {
Expand Down Expand Up @@ -837,7 +841,7 @@ export const t = /** @type {const} */ ({
yield "69"
return {
pattern: function Number(haystack, matches, verbose = false) {
return haystack != null && narrow_name(haystack) === "Number"
return haystack != null && (narrow_name(haystack) === "IntegerLiteral" || narrow_name(haystack) === "FloatLiteral")
},
}
},
Expand All @@ -846,9 +850,7 @@ export const t = /** @type {const} */ ({
yield `"A113"`
return {
pattern: function String(haystack, matches, verbose = false) {
return (
haystack != null && (narrow_name(haystack) === "StringWithoutInterpolation" || narrow_name(haystack) === "TripleStringWithoutInterpolation")
)
return haystack != null && (narrow_name(haystack) === "StringLiteral" || narrow_name(haystack) === "NsStringLiteral")
},
}
},
Expand Down
9 changes: 1 addition & 8 deletions frontend/components/CellInput/mixedParsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,7 @@ const overlayHack = (overlay, input) => {
})
}

const STRING_NODE_NAMES = new Set([
"TripleString",
"String",
"CommandString",
"TripleStringWithoutInterpolation",
"StringWithoutInterpolation",
"CommandStringWithoutInterpolation",
])
export const STRING_NODE_NAMES = new Set(["StringLiteral", "CommandLiteral", "NsStringLiteral", "NsCommandLiteral"])

const juliaWrapper = parseMixed((node, input) => {
if (!STRING_NODE_NAMES.has(node.type.name)) {
Expand Down
4 changes: 2 additions & 2 deletions frontend/components/CellInput/pkg_bubble_plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function find_import_statements({ doc, tree, from, to }) {
quotelevel++
}
// `$(...)` when inside quote
if (cursor.name === "InterpolationExpression") {
if (cursor.name === "InterpExpression") {
quotelevel--
}
if (quotelevel !== 0) return
Expand Down Expand Up @@ -160,7 +160,7 @@ function find_import_statements({ doc, tree, from, to }) {
if (cursor.name === "QuoteExpression" || cursor.name === "QuoteStatement") {
quotelevel--
}
if (cursor.name === "InterpolationExpression") {
if (cursor.name === "InterpExpression") {
quotelevel++
}
},
Expand Down
14 changes: 7 additions & 7 deletions frontend/components/CellInput/pluto_autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ScopeStateField } from "./scopestate_statefield.js"
import { open_bottom_right_panel } from "../BottomRightPanel.js"
import { ENABLE_CM_AUTOCOMPLETE_ON_TYPE } from "../CellInput.js"
import { GlobalDefinitionsFacet } from "./go_to_definition_plugin.js"
import { STRING_NODE_NAMES } from "./mixedParsers.js"

let { autocompletion, completionKeymap, completionStatus, acceptCompletion, selectedCompletion } = autocomplete

Expand Down Expand Up @@ -128,7 +129,7 @@ const match_symbol_complete = (/** @type {autocomplete.CompletionContext} */ ctx
function match_string_complete(state, pos) {
const tree = syntaxTree(state)
const node = tree.resolve(pos)
if (node == null || (node.name !== "TripleString" && node.name !== "String")) {
if (node == null || !STRING_NODE_NAMES.has(node.name)) {
return false
}
return true
Expand Down Expand Up @@ -166,7 +167,7 @@ const julia_code_completions_to_cm =
(/** @type {PlutoRequestAutocomplete} */ request_autocomplete) => async (/** @type {autocomplete.CompletionContext} */ ctx) => {
if (match_special_symbol_complete(ctx)) return null
if (!ctx.explicit && writing_variable_name_or_keyword(ctx)) return null
if (!ctx.explicit && ctx.tokenBefore(["Number", "Comment", "String", "TripleString"]) != null) return null
if (!ctx.explicit && ctx.tokenBefore(["IntegerLiteral", "FloatLiteral", "LineComment", "BlockComment", ...STRING_NODE_NAMES]) != null) return null

let to_complete = /** @type {String} */ (ctx.state.sliceDoc(0, ctx.pos))

Expand Down Expand Up @@ -270,13 +271,12 @@ const julia_code_completions_to_cm =
const complete_anyword = async (/** @type {autocomplete.CompletionContext} */ ctx) => {
if (match_special_symbol_complete(ctx)) return null
if (!ctx.explicit && writing_variable_name_or_keyword(ctx)) return null
if (!ctx.explicit && ctx.tokenBefore(["Number", "Comment", "String", "TripleString"]) != null) return null
if (!ctx.explicit && ctx.tokenBefore(["IntegerLiteral", "FloatLiteral", "LineComment", "BlockComment", ...STRING_NODE_NAMES]) != null) return null

const results_from_cm = await autocomplete.completeAnyWord(ctx)
if (results_from_cm === null) return null

const last_token = ctx.tokenBefore(["Identifier", "Number"])
if (last_token == null || last_token.type?.name === "Number") return null
if (ctx.tokenBefore(["Identifier", "IntegerLiteral", "FloatLiteral"])) return null

return {
from: results_from_cm.from,
Expand Down Expand Up @@ -321,7 +321,7 @@ const writing_variable_name_or_keyword = (/** @type {autocomplete.CompletionCont
const global_variables_completion = async (/** @type {autocomplete.CompletionContext} */ ctx) => {
if (match_special_symbol_complete(ctx)) return null
if (!ctx.explicit && writing_variable_name_or_keyword(ctx)) return null
if (!ctx.explicit && ctx.tokenBefore(["Number", "Comment", "String", "TripleString"]) != null) return null
if (!ctx.explicit && ctx.tokenBefore(["IntegerLiteral", "FloatLiteral", "LineComment", "BlockComment", ...STRING_NODE_NAMES]) != null) return null

const globals = ctx.state.facet(GlobalDefinitionsFacet)

Expand Down Expand Up @@ -438,7 +438,7 @@ const special_symbols_completion = (/** @type {() => Promise<SpecialSymbols?>} *
return async (/** @type {autocomplete.CompletionContext} */ ctx) => {
if (!match_special_symbol_complete(ctx)) return null
if (!ctx.explicit && writing_variable_name_or_keyword(ctx)) return null
if (!ctx.explicit && ctx.tokenBefore(["Number", "Comment"]) != null) return null
if (!ctx.explicit && ctx.tokenBefore(["IntegerLiteral", "FloatLiteral", "LineComment", "BlockComment"]) != null) return null

const result = await get_special_symbols()
return await autocomplete.completeFromList(result ?? [])(ctx)
Expand Down
23 changes: 12 additions & 11 deletions frontend/components/CellInput/scopestate_statefield.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ let merge_scope_state = (a, b) => {
/** @param {TreeCursor} cursor */
let search_for_interpolations = function* (cursor) {
for (let child of child_cursors(cursor)) {
if (child.name === "InterpolationExpression") {
if (child.name === "InterpExpression") {
yield cursor
} else if (child.name === "QuoteExpression" || child.name === "QuoteStatement") {
for (let child_child of search_for_interpolations(child)) {
Expand Down Expand Up @@ -470,36 +470,37 @@ export let explore_variable_usage = (
// Doing these checks in front seems to speed things up a bit.
if (
cursor.type.is("keyword") ||
cursor.name === "SourceFile" ||
cursor.name === "BooleanLiteral" ||
cursor.name === "Character" ||
cursor.name === "Program" ||
cursor.name === "BoolLiteral" ||
cursor.name === "CharLiteral" ||
cursor.name === "String" ||
cursor.name === "Number" ||
cursor.name === "IntegerLiteral" ||
cursor.name === "FloatLiteral" ||
cursor.name === "Comment" ||
cursor.name === "BinaryExpression" ||
cursor.name === "Operator" ||
cursor.name === "MacroArgumentList" ||
cursor.name === "ReturnStatement" ||
cursor.name === "BareTupleExpression" ||
cursor.name === "ParenthesizedExpression" ||
cursor.name === "OpenTuple" ||
cursor.name === "ParenExpression" ||
cursor.name === "Type" ||
cursor.name === "InterpolationExpression" ||
cursor.name === "InterpExpression" ||
cursor.name === "SpreadExpression" ||
cursor.name === "CompoundExpression" ||
cursor.name === "ParameterizedIdentifier" ||
cursor.name === "TypeArgumentList" ||
cursor.name === "BraceExpression" ||
cursor.name === "TernaryExpression" ||
cursor.name === "Coefficient" ||
cursor.name === "TripleString" ||
cursor.name === "RangeExpression" ||
cursor.name === "SubscriptExpression" ||
cursor.name === "IndexExpression" ||
cursor.name === "UnaryExpression" ||
cursor.name === "ConstStatement" ||
cursor.name === "GlobalStatement" ||
cursor.name === "ContinueStatement" ||
cursor.name === "MatrixExpression" ||
cursor.name === "MatrixRow" ||
cursor.name === "ArrayExpression"
cursor.name === "VectorExpression"
) {
for (let subcursor of child_cursors(cursor)) {
scopestate = explore_variable_usage(subcursor, doc, scopestate, verbose)
Expand Down

0 comments on commit 2478e62

Please sign in to comment.