Skip to content

Commit b8408a5

Browse files
committed
Enhance code syntax highlighting in documentation
1 parent d3c7a1e commit b8408a5

File tree

4 files changed

+125
-44
lines changed

4 files changed

+125
-44
lines changed

.idea/AugmentWebviewStateStore.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jline-docs/docs/advanced/interactive-features.md

Lines changed: 51 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ One of JLine's most powerful features is the ability to print text above the cur
1414

1515
The simplest way to print above the current line is to use the `printAbove` method of the `LineReader` class:
1616

17-
```java
17+
```java title="PrintAboveExample.java"
1818
import org.jline.reader.LineReader;
1919
import org.jline.reader.LineReaderBuilder;
2020
import org.jline.terminal.Terminal;
@@ -26,19 +26,20 @@ public class PrintAboveExample {
2626
LineReader reader = LineReaderBuilder.builder()
2727
.terminal(terminal)
2828
.build();
29-
29+
3030
// Start a background thread to print messages
3131
new Thread(() -> {
3232
try {
3333
for (int i = 0; i < 10; i++) {
3434
Thread.sleep(1000);
35+
// highlight-next-line
3536
reader.printAbove("Notification #" + i);
3637
}
3738
} catch (Exception e) {
3839
e.printStackTrace();
3940
}
4041
}).start();
41-
42+
4243
// Read input normally
4344
while (true) {
4445
String line = reader.readLine("prompt> ");
@@ -54,7 +55,7 @@ In this example, notifications will appear above the input line, and the user ca
5455

5556
For more control, you can use the `PrintAboveWriter` class:
5657

57-
```java
58+
```java title="PrintAboveWriterExample.java"
5859
import org.jline.reader.LineReader;
5960
import org.jline.reader.LineReaderBuilder;
6061
import org.jline.terminal.Terminal;
@@ -72,24 +73,26 @@ public class PrintAboveWriterExample {
7273
LineReader reader = LineReaderBuilder.builder()
7374
.terminal(terminal)
7475
.build();
75-
76+
77+
// highlight-start
7678
// Create a PrintAboveWriter
77-
PrintWriter writer = new PrintAboveWriter(reader.getTerminal(),
79+
PrintWriter writer = new PrintAboveWriter(reader.getTerminal(),
7880
reader::printAbove);
79-
81+
// highlight-end
82+
8083
// Start a background thread to print messages
8184
new Thread(() -> {
8285
try {
8386
for (int i = 0; i < 10; i++) {
8487
Thread.sleep(1000);
85-
88+
8689
// Create a styled message
8790
AttributedStringBuilder asb = new AttributedStringBuilder();
8891
asb.style(AttributedStyle.DEFAULT.foreground(AttributedStyle.GREEN))
8992
.append("Notification #")
9093
.append(String.valueOf(i))
9194
.style(AttributedStyle.DEFAULT);
92-
95+
9396
// Print the message above the current line
9497
writer.println(asb.toAnsi(terminal));
9598
writer.flush();
@@ -98,7 +101,7 @@ public class PrintAboveWriterExample {
98101
e.printStackTrace();
99102
}
100103
}).start();
101-
104+
102105
// Read input normally
103106
while (true) {
104107
String line = reader.readLine("prompt> ");
@@ -126,7 +129,7 @@ JLine's Status feature allows you to display persistent status information at th
126129

127130
### Basic Status Usage
128131

129-
```java
132+
```java title="StatusExample.java" showLineNumbers
130133
import org.jline.reader.LineReader;
131134
import org.jline.reader.LineReaderBuilder;
132135
import org.jline.terminal.Terminal;
@@ -141,19 +144,21 @@ public class StatusExample {
141144
LineReader reader = LineReaderBuilder.builder()
142145
.terminal(terminal)
143146
.build();
144-
147+
145148
// Create a Status instance
146149
Status status = Status.getStatus(terminal);
147150
if (status != null) {
151+
// highlight-start
148152
// Update the status line
149153
status.update(new AttributedStringBuilder()
150154
.style(AttributedStyle.DEFAULT.foreground(AttributedStyle.BLUE))
151155
.append("Connected to server | ")
152156
.style(AttributedStyle.DEFAULT.foreground(AttributedStyle.GREEN))
153157
.append("3 tasks running")
154158
.toAttributedString());
159+
// highlight-end
155160
}
156-
161+
157162
// Read input normally
158163
while (true) {
159164
String line = reader.readLine("prompt> ");
@@ -175,7 +180,7 @@ new Thread(() -> {
175180
while (true) {
176181
Thread.sleep(2000);
177182
taskCount = (taskCount + 1) % 10;
178-
183+
179184
if (status != null) {
180185
status.update(new AttributedStringBuilder()
181186
.style(AttributedStyle.DEFAULT.foreground(AttributedStyle.BLUE))
@@ -225,7 +230,7 @@ Tailtips provide contextual hints or suggestions that appear after the cursor. T
225230

226231
### Basic Tailtips Usage
227232

228-
```java
233+
```java title="TailtipExample.java" showLineNumbers
229234
import org.jline.reader.LineReader;
230235
import org.jline.reader.LineReaderBuilder;
231236
import org.jline.reader.impl.DefaultParser;
@@ -238,17 +243,19 @@ public class TailtipExample {
238243
public static void main(String[] args) throws Exception {
239244
Terminal terminal = TerminalBuilder.builder().build();
240245
DefaultParser parser = new DefaultParser();
241-
246+
242247
LineReader reader = LineReaderBuilder.builder()
243248
.terminal(terminal)
244249
.parser(parser)
250+
// highlight-next-line
245251
.variable(LineReader.TAILTIP_ENABLED, true)
246252
.build();
247-
253+
248254
// Read input with tailtips
249255
while (true) {
250-
String line = reader.readLine("prompt> ", null,
251-
(String) null, null,
256+
// error-start
257+
String line = reader.readLine("prompt> ", null,
258+
(String) null, null,
252259
s -> {
253260
// This function provides the tailtip based on current input
254261
if (s.startsWith("help")) {
@@ -264,7 +271,8 @@ public class TailtipExample {
264271
}
265272
return null;
266273
});
267-
274+
// error-end
275+
268276
System.out.println("You entered: " + line);
269277
}
270278
}
@@ -285,13 +293,13 @@ commandHelp.put("list", "[pattern] - List available resources");
285293

286294
// Read input with command-specific tailtips
287295
while (true) {
288-
String line = reader.readLine("prompt> ", null,
289-
(String) null, null,
296+
String line = reader.readLine("prompt> ", null,
297+
(String) null, null,
290298
s -> {
291299
// Extract the command part
292300
String[] parts = s.split("\\s+", 2);
293301
String cmd = parts[0];
294-
302+
295303
// Look up help for this command
296304
String help = commandHelp.get(cmd);
297305
if (help != null) {
@@ -302,7 +310,7 @@ while (true) {
302310
}
303311
return null;
304312
});
305-
313+
306314
System.out.println("You entered: " + line);
307315
}
308316
```
@@ -314,19 +322,19 @@ You can provide more sophisticated tailtips based on the current parsing context
314322
```java
315323
// Read input with context-aware tailtips
316324
while (true) {
317-
String line = reader.readLine("prompt> ", null,
318-
(String) null, null,
325+
String line = reader.readLine("prompt> ", null,
326+
(String) null, null,
319327
s -> {
320328
try {
321329
// Parse the current line
322330
ParsedLine pl = parser.parse(s, s.length());
323331
String word = pl.word();
324332
List<String> words = pl.words();
325-
333+
326334
// Command-specific help based on context
327335
if (words.size() >= 1) {
328336
String cmd = words.get(0);
329-
337+
330338
if (cmd.equals("connect")) {
331339
if (words.size() == 1) {
332340
// Just the command
@@ -348,7 +356,7 @@ while (true) {
348356
}
349357
return null;
350358
});
351-
359+
352360
System.out.println("You entered: " + line);
353361
}
354362
```
@@ -376,16 +384,16 @@ public class InteractiveExample {
376384
public static void main(String[] args) throws Exception {
377385
Terminal terminal = TerminalBuilder.builder().build();
378386
DefaultParser parser = new DefaultParser();
379-
387+
380388
LineReader reader = LineReaderBuilder.builder()
381389
.terminal(terminal)
382390
.parser(parser)
383391
.variable(LineReader.TAILTIP_ENABLED, true)
384392
.build();
385-
393+
386394
// Set up PrintAboveWriter
387395
PrintWriter writer = new PrintAboveWriter(terminal, reader::printAbove);
388-
396+
389397
// Set up Status
390398
Status status = Status.getStatus(terminal);
391399
if (status != null) {
@@ -394,29 +402,29 @@ public class InteractiveExample {
394402
.append("Ready")
395403
.toAttributedString());
396404
}
397-
405+
398406
// Command help for tailtips
399407
Map<String, String> commandHelp = new HashMap<>();
400408
commandHelp.put("help", "[command] - Display help for command");
401409
commandHelp.put("connect", "<host> <port> - Connect to server");
402410
commandHelp.put("disconnect", "- Disconnect from server");
403411
commandHelp.put("list", "[pattern] - List available resources");
404-
412+
405413
// Start a background thread for notifications
406414
new Thread(() -> {
407415
try {
408416
for (int i = 0; i < 10; i++) {
409417
Thread.sleep(3000);
410-
418+
411419
// Print notification above
412420
AttributedStringBuilder asb = new AttributedStringBuilder();
413421
asb.style(AttributedStyle.DEFAULT.foreground(AttributedStyle.GREEN))
414422
.append("System notification #")
415423
.append(String.valueOf(i));
416-
424+
417425
writer.println(asb.toAnsi(terminal));
418426
writer.flush();
419-
427+
420428
// Update status
421429
if (status != null) {
422430
status.update(new AttributedStringBuilder()
@@ -431,16 +439,16 @@ public class InteractiveExample {
431439
e.printStackTrace();
432440
}
433441
}).start();
434-
442+
435443
// Main input loop with tailtips
436444
while (true) {
437-
String line = reader.readLine("prompt> ", null,
438-
(String) null, null,
445+
String line = reader.readLine("prompt> ", null,
446+
(String) null, null,
439447
s -> {
440448
// Extract the command part
441449
String[] parts = s.split("\\s+", 2);
442450
String cmd = parts[0];
443-
451+
444452
// Look up help for this command
445453
String help = commandHelp.get(cmd);
446454
if (help != null) {
@@ -451,9 +459,9 @@ public class InteractiveExample {
451459
}
452460
return null;
453461
});
454-
462+
455463
System.out.println("You entered: " + line);
456-
464+
457465
// Update status based on command
458466
if (status != null) {
459467
status.update(new AttributedStringBuilder()

jline-docs/docusaurus.config.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,21 @@ const config: Config = {
118118
prism: {
119119
theme: prismThemes.github,
120120
darkTheme: prismThemes.dracula,
121+
additionalLanguages: ['java', 'bash', 'diff', 'json', 'xml'],
122+
defaultLanguage: 'java',
123+
magicComments: [
124+
// Remember to extend the default highlight class name as well!
125+
{
126+
className: 'theme-code-block-highlighted-line',
127+
line: 'highlight-next-line',
128+
block: {start: 'highlight-start', end: 'highlight-end'},
129+
},
130+
{
131+
className: 'code-block-error-line',
132+
line: 'error-next-line',
133+
block: {start: 'error-start', end: 'error-end'},
134+
},
135+
],
121136
},
122137
} satisfies Preset.ThemeConfig,
123138
};

jline-docs/src/css/custom.css

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,61 @@
2828
--ifm-color-primary-lightest: #4fddbf;
2929
--docusaurus-highlighted-code-line-bg: rgba(0, 0, 0, 0.3);
3030
}
31+
32+
/* Enhanced code block styling */
33+
.theme-code-block {
34+
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
35+
border-radius: 8px;
36+
margin-bottom: 1.5rem;
37+
}
38+
39+
.theme-code-block pre {
40+
font-family: 'JetBrains Mono', 'Fira Code', 'Roboto Mono', 'Source Code Pro', monospace;
41+
font-size: 0.9rem;
42+
line-height: 1.5;
43+
padding: 1rem;
44+
}
45+
46+
/* Line highlighting */
47+
.theme-code-block-highlighted-line {
48+
background-color: rgba(46, 133, 85, 0.15);
49+
display: block;
50+
margin: 0 -1rem;
51+
padding: 0 1rem;
52+
border-left: 3px solid var(--ifm-color-primary);
53+
}
54+
55+
[data-theme='dark'] .theme-code-block-highlighted-line {
56+
background-color: rgba(37, 194, 160, 0.15);
57+
}
58+
59+
/* Error line highlighting */
60+
.code-block-error-line {
61+
background-color: rgba(255, 76, 76, 0.15);
62+
display: block;
63+
margin: 0 -1rem;
64+
padding: 0 1rem;
65+
border-left: 3px solid #ff4c4c;
66+
}
67+
68+
/* Improve code block titles */
69+
.theme-code-block-highlighted-title {
70+
background-color: var(--ifm-color-primary);
71+
color: white;
72+
font-weight: bold;
73+
padding: 0.5rem 1rem;
74+
border-top-left-radius: 8px;
75+
border-top-right-radius: 8px;
76+
}
77+
78+
/* Improve inline code */
79+
code {
80+
background-color: rgba(46, 133, 85, 0.1);
81+
padding: 0.2rem 0.4rem;
82+
border-radius: 4px;
83+
font-family: 'JetBrains Mono', 'Fira Code', 'Roboto Mono', 'Source Code Pro', monospace;
84+
}
85+
86+
[data-theme='dark'] code {
87+
background-color: rgba(37, 194, 160, 0.1);
88+
}

0 commit comments

Comments
 (0)