mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 08:48:11 +00:00
Spreadsheet: Show a small inline doc window for the "current" function
If the user is typing in the cell editor and has the cursor in a function call, try to show a tip for the arguments of that function: (cursor denoted by `|`) ``` sum(| ``` should show: ``` sum(cell names) ``` in a tooltip-like window below the editor.
This commit is contained in:
parent
aa5b43a2bc
commit
f606e78556
6 changed files with 180 additions and 3 deletions
|
@ -27,6 +27,7 @@
|
|||
#include "JSIntegration.h"
|
||||
#include "Spreadsheet.h"
|
||||
#include "Workbook.h"
|
||||
#include <LibJS/Lexer.h>
|
||||
#include <LibJS/Runtime/Error.h>
|
||||
#include <LibJS/Runtime/GlobalObject.h>
|
||||
#include <LibJS/Runtime/Object.h>
|
||||
|
@ -34,6 +35,78 @@
|
|||
|
||||
namespace Spreadsheet {
|
||||
|
||||
Optional<FunctionAndArgumentIndex> get_function_and_argument_index(StringView source)
|
||||
{
|
||||
JS::Lexer lexer { source };
|
||||
// Track <identifier> <OpenParen>'s, and how many complete expressions are inside the parenthesised expression.
|
||||
Vector<size_t> state;
|
||||
StringView last_name;
|
||||
Vector<StringView> names;
|
||||
size_t open_parens_since_last_commit = 0;
|
||||
size_t open_curlies_and_brackets_since_last_commit = 0;
|
||||
bool previous_was_identifier = false;
|
||||
auto token = lexer.next();
|
||||
while (token.type() != JS::TokenType::Eof) {
|
||||
switch (token.type()) {
|
||||
case JS::TokenType::Identifier:
|
||||
previous_was_identifier = true;
|
||||
last_name = token.value();
|
||||
break;
|
||||
case JS::TokenType::ParenOpen:
|
||||
if (!previous_was_identifier) {
|
||||
open_parens_since_last_commit++;
|
||||
break;
|
||||
}
|
||||
previous_was_identifier = false;
|
||||
state.append(0);
|
||||
names.append(last_name);
|
||||
break;
|
||||
case JS::TokenType::ParenClose:
|
||||
previous_was_identifier = false;
|
||||
if (open_parens_since_last_commit == 0) {
|
||||
state.take_last();
|
||||
names.take_last();
|
||||
break;
|
||||
}
|
||||
--open_parens_since_last_commit;
|
||||
break;
|
||||
case JS::TokenType::Comma:
|
||||
previous_was_identifier = false;
|
||||
if (open_parens_since_last_commit == 0 && open_curlies_and_brackets_since_last_commit == 0) {
|
||||
state.last()++;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case JS::TokenType::BracketOpen:
|
||||
previous_was_identifier = false;
|
||||
open_curlies_and_brackets_since_last_commit++;
|
||||
break;
|
||||
case JS::TokenType::BracketClose:
|
||||
previous_was_identifier = false;
|
||||
if (open_curlies_and_brackets_since_last_commit > 0)
|
||||
open_curlies_and_brackets_since_last_commit--;
|
||||
break;
|
||||
case JS::TokenType::CurlyOpen:
|
||||
previous_was_identifier = false;
|
||||
open_curlies_and_brackets_since_last_commit++;
|
||||
break;
|
||||
case JS::TokenType::CurlyClose:
|
||||
previous_was_identifier = false;
|
||||
if (open_curlies_and_brackets_since_last_commit > 0)
|
||||
open_curlies_and_brackets_since_last_commit--;
|
||||
break;
|
||||
default:
|
||||
previous_was_identifier = false;
|
||||
break;
|
||||
}
|
||||
|
||||
token = lexer.next();
|
||||
}
|
||||
if (!names.is_empty() && !state.is_empty())
|
||||
return FunctionAndArgumentIndex { names.last(), state.last() };
|
||||
return {};
|
||||
}
|
||||
|
||||
SheetGlobalObject::SheetGlobalObject(Sheet& sheet)
|
||||
: m_sheet(sheet)
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue