Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add APIs, commands and tests for syntax-highlighting in the REPL #2291

Merged
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
7aa2a2d
feat: add APIs to programmatically control syntax-highlighter
Snehil-Shah Jun 1, 2024
31b5134
feat: add REPL commands to control syntax-highlighting
Snehil-Shah Jun 1, 2024
6858433
feat: add setting to enable/disable syntax-highlighting
Snehil-Shah Jun 1, 2024
0c3e6d3
test: fix failing tests
Snehil-Shah Jun 1, 2024
d5a00a8
feat: add options for syntax-highlighting upon REPL startup
Snehil-Shah Jun 1, 2024
56c30bf
refactor: update API and command signatures
Snehil-Shah Jun 2, 2024
f466ab3
refactor: move `await` keyword to a control keyword
Snehil-Shah Jun 2, 2024
1fd6070
fix: resolve `null` try parameters crashing the REPL
Snehil-Shah Jun 2, 2024
f8c10a4
fix: don't throw error if a node doesn't match a declaration
Snehil-Shah Jun 2, 2024
5c9b7f6
fix: handle member expressions like `foo['bar']`
Snehil-Shah Jun 2, 2024
3e7d7ba
test: add tests for the syntax highlighter
Snehil-Shah Jun 2, 2024
570f24e
refactor: clean code logic
Snehil-Shah Jun 2, 2024
7f31a89
docs: document added prototype methods and settings
Snehil-Shah Jun 2, 2024
028f477
test: update test case
Snehil-Shah Jun 2, 2024
7c99ef7
fix: handle recursive `MemberExpressions`
Snehil-Shah Jun 3, 2024
1b79a3c
refactor: clean code logic
Snehil-Shah Jun 3, 2024
765c19b
fix: correct string formatting
Snehil-Shah Jun 3, 2024
4480879
fix: ignore placeholder nodes
Snehil-Shah Jun 3, 2024
0ea6e2e
fix: setting themes during startup
Snehil-Shah Jun 4, 2024
de9e1e8
docs: keep it consistent
Snehil-Shah Jun 4, 2024
802d602
refactor: return theme object in `getTheme` command
Snehil-Shah Jun 4, 2024
4e2af9a
fix: suggestions
Snehil-Shah Jun 5, 2024
7bc6f92
Apply suggestions from code review
kgryte Jun 7, 2024
7c7dbeb
Apply suggestions from code review
kgryte Jun 7, 2024
3f7d885
Apply suggestions from code review
kgryte Jun 7, 2024
3d80c69
Apply suggestions from code review
kgryte Jun 7, 2024
8938c74
Apply suggestions from code review
kgryte Jun 7, 2024
7de50f5
Apply suggestions from code review
kgryte Jun 7, 2024
3798c65
Apply suggestions from code review
kgryte Jun 7, 2024
ba7ac55
fix: suggestions
Snehil-Shah Jun 8, 2024
81bdca7
Apply suggestions from code review
kgryte Jun 8, 2024
6ea845f
Apply suggestions from code review
kgryte Jun 8, 2024
6913e06
Apply suggestions from code review
kgryte Jun 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
266 changes: 265 additions & 1 deletion lib/node_modules/@stdlib/repl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ The function accepts the following `options`:
- **outputPrompt**: output prompt. If the output prompt includes the character sequence `%d`, the output prompt includes line numbers. Default: `'Out[%d]: '`.
- **welcome**: welcome message.
- **padding**: number of empty lines between consecutive commands. Default: `1`.
- **themes**: table containing color themes for syntax highlighting.
- **load**: file path specifying a JavaScript file to load and evaluate line-by-line (e.g., a previous REPL history file).
- **save**: file path specifying where to save REPL command history.
- **log**: file path specifying where to save REPL commands and printed output.
Expand All @@ -83,6 +84,27 @@ The function supports specifying the following settings:
- **autoDeletePairs**: boolean indicating whether to automatically delete adjacent matching brackets, parentheses, and quotes. Default: `true`.
- **autoPage**: boolean indicating whether to automatically page return values having a display size exceeding the visible screen. When streams are TTY, the default is `true`; otherwise, the default is `false`.
- **completionPreviews**: boolean indicating whether to display completion previews for auto-completion. When streams are TTY, the default is `true`; otherwise, the default is `false`.
- **syntaxHighlighting**: boolean indicating whether to enable syntax highlighting of entered input expressions. When streams are TTY, the default is `true`; otherwise, the default is `false`.
- **theme**: initial color theme for syntax highlighting. Default: `stdlib-ansi-basic`.

#### REPL.prototype.viewport()

Returns the REPL viewport.

```javascript
var debug = require( '@stdlib/streams/node/debug-sink' );

// Create a new REPL:
var repl = new REPL({
'output': debug()
});

// Query the REPL viewport:
var v = repl.viewport();

// Close the REPL:
repl.close();
```

#### REPL.prototype.createContext()

Expand Down Expand Up @@ -153,7 +175,7 @@ repl.clearHistory();
repl.close();
```

#### Repl.prototype.clearUserDocs()
#### REPL.prototype.clearUserDocs()

Clears user-defined documentation.

Expand All @@ -176,6 +198,167 @@ repl.clearUserDocs();
repl.close();
```

#### REPL.prototype.themes()

Returns a list of all available themes for syntax highlighting.

```javascript
var debug = require( '@stdlib/streams/node/debug-sink' );

// Create a new REPL:
var repl = new REPL({
'output': debug()
});

// ...

// Fetch all available themes:
var themes = repl.themes();
// returns [...]

// ...

// Close the REPL:
repl.close();
```

#### REPL.prototype.getTheme()
Snehil-Shah marked this conversation as resolved.
Show resolved Hide resolved

Returns a theme's color palette for syntax highlighting.

```javascript
var debug = require( '@stdlib/streams/node/debug-sink' );

// Create a new REPL:
var repl = new REPL({
'output': debug()
});

// ...

// Add a user-defined theme:
repl.addTheme( 'myTheme', {
'keyword': 'red'
});

// Get a theme's color palette:
var theme = repl.getTheme( 'myTheme' );
// returns { 'keyword': 'red' }

// ...

// Close the REPL:
repl.close();
```

#### REPL.prototype.addTheme()

Adds a syntax highlighting theme.

```javascript
var debug = require( '@stdlib/streams/node/debug-sink' );

// Create a new REPL:
var repl = new REPL({
'output': debug()
});

// ...

// Add a user-defined theme:
repl.addTheme( 'myTheme', {
'keyword': 'red',
'variable': 'green'

// ...
});

// ...

// Close the REPL:
repl.close();
```

kgryte marked this conversation as resolved.
Show resolved Hide resolved
The syntax-highlighter supports the following tokens and associated theme fields:

- **keyword**: keywords (e.g., `var`, `function`, `let`, `const`, `in`, and `class`).
- **control**: control flow keywords (e.g., `if`, `else`, `try`, `catch`, and `return`).
- **specialIdentifier**: special identifiers (e.g., `this` and `super`).
- **string**: string and template literals.
- **number**: numeric literals.
- **literal**: reserved literals (e.g., `true`, `false`, `null`, and `undefined`).
- **regexp**: regular expressions.
- **command**: built-in REPL commands.
- **function**: function identifiers.
- **object**: object identifiers.
- **variable**: literal identifiers.
- **name**: variable names.
- **comment**: line comments.
- **punctuation**: punctuation symbols (e.g., `;`, `[`, `{`, `,`, and `?`).
- **operator**: operator symbols (e.g., `+`, `-`, `*`, `=`, `++`, `>=`, and `&&`).

#### REPL.prototype.deleteTheme()

Deletes a specified theme from the syntax-highlighter.

```javascript
var debug = require( '@stdlib/streams/node/debug-sink' );

// Create a new REPL:
var repl = new REPL({
'output': debug()
});

// ...
Snehil-Shah marked this conversation as resolved.
Show resolved Hide resolved

// Add a user-defined theme:
repl.addTheme( 'myTheme', {
'keyword': 'red',
'variable': 'green'

// ...
});

// Delete the added theme:
repl.deleteTheme( 'myTheme' );

// ...

// Close the REPL:
repl.close();
```

#### REPL.prototype.renameTheme()

Renames a specified theme in the syntax-highlighter.

```javascript
var debug = require( '@stdlib/streams/node/debug-sink' );

// Create a new REPL:
var repl = new REPL({
'output': debug()
});

// ...

// Add a user-defined theme:
repl.addTheme( 'myTheme', {
'keyword': 'red',
'variable': 'green'

// ...
});

// Rename the added theme:
repl.renameTheme( 'myTheme', 'yourTheme' );
Snehil-Shah marked this conversation as resolved.
Show resolved Hide resolved

// ...

// Close the REPL:
repl.close();
```

Snehil-Shah marked this conversation as resolved.
Show resolved Hide resolved
#### REPL.prototype.load( fpath, clbk )

Loads and evaluates a JavaScript file line-by-line.
Expand Down Expand Up @@ -386,6 +569,19 @@ repl.close();

REPL instances support the following commands...

#### addTheme( name, theme )

Adds a syntax highlighting color theme.

```text
// Add color theme:
In [1]: addTheme( 'myTheme', { 'keyword': 'red' } )

// Check updated list of themes:
In [2]: themes()
Out[2]: [ 'stdlib-ansi-basic', 'myTheme' ]
```

#### alias2pkg( arg )

Returns the package name associated with a provided alias or class instance.
Expand Down Expand Up @@ -597,6 +793,26 @@ In [1]: currentWorkspace
Out[1]: 'base'
```

#### deleteTheme( name )

Deletes a syntax highlighting color theme.

```text
// Add a color theme:
In [1]: addTheme( 'myTheme', { 'keyword': 'red' } )

// Check list of themes:
In [2]: themes()
Out[2]: [ 'stdlib-ansi-basic', 'myTheme' ]

// Delete the added theme:
In [3]: deleteTheme( 'myTheme' )

// Check updated list of themes:
In [4]: themes()
Out[4]: [ 'stdlib-ansi-basic' ]
```

#### deeprerequire( id )

Re-imports a module, JSON, or local file and all of its associated module dependencies.
Expand Down Expand Up @@ -695,6 +911,21 @@ In [1]: example( base.sin )

**Note**: only direct instances of documented built-in constructors are supported.

#### getTheme( \[name] )

Returns a syntax highlighting color theme.

```text
// Add a color theme:
In [1]: addTheme( 'myTheme', { 'keyword': 'red' } )

// Get the color theme:
In [2]: getTheme( 'myTheme' )
Out[2]: { 'keyword': 'red' }
```

**Note**: if no theme name is provided, the current theme is returned.

#### help( \[arg] )

Prints help text.
Expand Down Expand Up @@ -876,6 +1107,26 @@ Exits the REPL.
In [1]: quit()
```

#### renameTheme( oldName, newName )

Renames a syntax highlighting color theme.

```text
// Add a color theme:
In [1]: addTheme( 'myTheme', { 'keyword': 'red' } )

// Check list of themes:
In [2]: themes()
Out[2]: [ 'stdlib-ansi-basic', 'myTheme' ]

// Rename the added theme:
In [3]: getTheme( 'myTheme', 'yourTheme' )

// Check updated list of themes:
In [4]: themes()
Out[4]: [ 'stdlib-ansi-basic', 'yourTheme' ]
```

#### renameWorkspace( oldName, newName )

Renames a workspace.
Expand Down Expand Up @@ -1059,6 +1310,19 @@ To update a specific setting, provide a `value` argument.
In [1]: settings( 'autoClosePairs', false )
```

#### themes()

Returns a list of all available syntax highlighting color themes.

```text
// Add a color theme:
In [1]: addTheme( 'myTheme', { 'keyword': 'red' } )

// Check list of themes:
In [2]: themes()
Out[2]: [ 'stdlib-ansi-basic', 'myTheme' ]
```

#### tutorial( \[name, \[options]] )

Starts a tutorial.
Expand Down
Loading
Loading