#! @file hover.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains hovering: what an editor shows of the name at a position, which is #! how what it refers to is declared, with the types the checker gave it and #! the contract a function's body begins with, and its documentation. import std::containers; module sakura::analyzer; #! What hovering a name shows. pub struct hover { pub var signature: string; #! How what it refers to is declared, as `def str::trim(this: &str): str` or `val count: int`, after the attributes written on it, and of a function, a line for each `expects` and `ensures` its body begins with. pub var documentation: string; #! The documentation comment of what it refers to, without its `#!`, or empty. pub var offset: u32; #! Where the name begins in the file's text. pub var length: u32; #! How many bytes it covers. } #! What hovering the name at the offset of the file shows: a variable, #! parameter, function, method, field, type, member of an enum, or module, #! used or declared there. #! @param this The checker. #! @param file The file. #! @param offset The byte of its text. #! @returns What it shows, or nothing where no name is there, or checking left #! it unresolved. pub def checker::hover(&mut this, file: u32, offset: u32): maybe { val token := this.token_at(file, offset); return nothing {} if token as u64 >= this.units[file].syntax.token_count; val asked := this.begin_query(file); var found := hover { signature = string::new(nothing {}), documentation = string::new(nothing {}) }; var shown := false; for &node in asked.walked.order { continue if this.token_of(node) != token; shown = this.shown_at(node, &asked.walked, &asked.decls, &mut found); break if shown; } return nothing {} if !shown; return move(&mut found); } #! What hovering a node of the file being queried shows, or nothing where it #! names nothing the checker resolved. #! @param this The checker. #! @param node The node. #! @param walked The file's tree. #! @param decls The declarations of the file's nodes. #! @param out Where what it shows is written. #! @returns Whether it shows anything. def checker::shown_at(&mut this, node: u32, walked: &tree_walk, decls: &dict, out: &mut hover): bool { val file := this.at.file; val spot := this.spot_of(file, this.token_of(node)); val meant := this.meaning_of(node, walked, decls); var signature := string::new(nothing {}); var documentation := string::new(nothing {}); if meant.is_module { signature = this.module_signature(meant.named_module); } else if meant.decl != 0 { val decl := meant.decl; val home: u32 = this.decls.files.p[decl]; val declared: u32 = this.decls.nodes.p[decl]; if declared != 0 { this.attributes_text(home, declared, &mut signature); } var written := this.signature_seen(decl, node, walked.parents[node]); signature.append(written.span); val contract := this.contract_of(decl); if contract.length != 0 { signature.append("\n"); signature.append(contract.span); } documentation = this.documentation_of(home, declared); this.query_file(file); } else if meant.local != 0 { signature = this.local_signature(meant.file, meant.local); documentation = this.documentation_of(meant.file, meant.local); } else if this.kind_of(node) == nk::member && this.type_at(node) != 0 && this.type_at(node) != bt::error { # What the compiler gives a value, as the length of a fixed array. signature.append("val "); signature.append(this.text_of(node)); signature.append(": "); this.type_text(this.type_at(node), &mut signature); } else { return false; } out.signature = move(&mut signature); out.documentation = move(&mut documentation); out.offset = spot.offset; out.length = spot.length; return true; } #! The contract a function's body begins with, which is what callers are #! promised: each `expects` and `ensures` before its first other statement, as #! it is written, with its spaces and line breaks run together, a line each, #! as `expects(b != 0)`. #! @param this The checker. #! @param decl The function. #! @returns The lines, or empty text where it begins with none. pub def checker::contract_of(&mut this, decl: u32): string { var made := string::new(nothing {}); return made if this.decls.kinds.p[decl] != dk::function || this.decls.has(decl, df::accessor); val file: u32 = this.decls.files.p[decl]; val syntax := &this.units[file].syntax; val source := &this.units[file].source; val node: u32 = this.decls.nodes.p[decl]; return made if node == 0 || syntax.node_kinds[node] != nk::function; val body := syntax.record(node, function_slot::body); return made if body == 0 || syntax.node_kinds[body] != nk::block; val run := syntax.node_lefts[body]; for i in 0..syntax.run_length(run) { val each := syntax.run_at(run, i as u32); break if syntax.node_kinds[each] != nk::contract; val first: u32 = syntax.node_tokens[each]; continue if syntax.token_names[first] == names::asserts_id; # What it was parsed from ends at its `;`, which is left out. var last: u32 = syntax.node_end(each); if last > first && syntax.token_kinds[last] == tk::semicolon { last--; } val begin: u64 = syntax.token_offsets[first]; val end: u64 = syntax.token_offsets[last] + syntax.token_length(last, this.names); continue if end <= begin; if made.length != 0 { made.append("\n"); } var spaced := false; for &byte in source.make_lexeme(begin, end - begin) { if byte == ' ' || byte == '\n' || byte == '\t' || byte == '\r' { spaced = true; continue; } if spaced && byte != ')' && made.length != 0 && made[made.length - 1] != '(' && made[made.length - 1] != '\n' { made.push(' '); } spaced = false; made.push(byte); } } return made; } #! The instance a call of a node uses, where the node is what a call calls. #! @param this The checker. #! @param callee The node. #! @returns The instance, or nothing. def checker::called_by(&mut this, callee: u32): maybe { # The call is found by its callee, which its node holds. val syntax := &this.syn(); for at in 1..syntax.node_count { val node := at as u32; continue if syntax.node_kinds[node] != nk::call || this.left_of(node) != callee; if this.called.get(&this.table_node_of(node)) is some(found) { return found; } return nothing {}; } return nothing {}; } #! How a declaration is written where a node names it: a field reached through #! a value of a generic type, as `p.x` of a `box`, is of the type the value #! gives it, and a method called through one is of the types it is given. #! @param this The checker. #! @param decl The declaration. #! @param node The node naming it, of the file being queried. #! @param parent The node holding it. #! @returns The signature. def checker::signature_seen(&mut this, decl: u32, node: u32, parent: u32): string { val kind := this.kind_of(node); val seen := this.type_at(node); # A generic function a call uses is shown as the instance the call uses. if this.decls.kinds.p[decl] == dk::function && !this.decls.has(decl, df::accessor) { # The call is of the node, or of the path whose last segment it is. var made: u32 = 0; var callee := node; if parent != 0 && kind == nk::segment && (this.kind_of(parent) == nk::path || this.kind_of(parent) == nk::type_name) { callee = parent; } if this.called.get(&this.table_node_of(callee)) is some(found) { made = found; } if made == 0 && callee != node { if this.called.get(&this.table_node_of(node)) is some(found) { made = found; } } if made == 0 { if this.called_by(callee) is some(found) { made = found; } } if made != 0 && made < this.instances.length as u32 && this.instances[made].decl == decl { var out := string::new(nothing {}); if this.decls.has(decl, df::exported) { out.append("pub "); } this.instance_text_of(decl, true, made, &mut out); return out; } } if (kind == nk::member || kind == nk::field_init) && seen != 0 && seen != bt::error && this.decls.kinds.p[decl] == dk::field && this.decls.types.p[decl] != seen { var out := string::new(nothing {}); if this.decls.has(decl, df::exported) { out.append("pub "); } out.append(this.decls.has(decl, df::mutable) ? "var " : "val "); out.append(this.decl_text(decl)); out.append(": "); this.type_text(seen, &mut out); return out; } return this.signature_of(decl); }