0
# Text Editing
1
2
The Groovy Console provides advanced text editing capabilities with Groovy-specific syntax highlighting, auto-completion, smart indentation, and comprehensive editing features.
3
4
## Main Text Editor
5
6
```groovy { .api }
7
class ConsoleTextEditor extends JScrollPane {
8
// Constructor
9
ConsoleTextEditor()
10
11
// Core Editor Access
12
TextEditor getTextEditor()
13
14
// Display Configuration
15
void setShowLineNumbers(boolean showLineNumbers)
16
void setEditable(boolean editable)
17
18
// Clipboard Operations
19
boolean clipBoardAvailable()
20
21
// Actions
22
Action getUndoAction()
23
Action getRedoAction()
24
Action getPrintAction()
25
26
// Syntax Highlighting
27
void enableHighLighter(Class clazz)
28
}
29
```
30
31
### Parameters
32
33
**setShowLineNumbers():**
34
- `showLineNumbers` (boolean): Whether to display line numbers in the editor
35
36
**setEditable():**
37
- `editable` (boolean): Whether the editor content can be modified
38
39
**enableHighLighter():**
40
- `clazz` (Class): Highlighter class to enable for syntax highlighting
41
42
### Returns
43
44
- **getTextEditor()**: TextEditor - The underlying text editor component
45
- **clipBoardAvailable()**: boolean - True if clipboard operations are available
46
- **getUndoAction()**: Action - Swing Action for undo operation
47
- **getRedoAction()**: Action - Swing Action for redo operation
48
- **getPrintAction()**: Action - Swing Action for print operation
49
50
## Base Text Editor
51
52
```groovy { .api }
53
class TextEditor extends JTextPane implements Pageable, Printable {
54
// Constructor
55
TextEditor()
56
57
// Implements Pageable and Printable for printing support
58
}
59
```
60
61
The TextEditor class provides the core text editing functionality and printing capabilities.
62
63
## Syntax Highlighting and Filtering
64
65
```groovy { .api }
66
class GroovyFilter extends StructuredSyntaxDocumentFilter {
67
// Groovy-specific syntax filtering and highlighting
68
}
69
70
class StructuredSyntaxDocumentFilter extends DocumentFilter {
71
// Base class for syntax highlighting document filters
72
}
73
74
class SmartDocumentFilter extends DocumentFilter {
75
// Smart editing features like auto-indentation and bracket matching
76
}
77
```
78
79
## Text Editor Features
80
81
### Undo/Redo Management
82
83
```groovy { .api }
84
class TextUndoManager extends UndoManager {
85
// Enhanced undo manager for text operations
86
}
87
```
88
89
### Bracket Matching
90
91
```groovy { .api }
92
class MatchingHighlighter implements CaretListener {
93
// Highlights matching brackets, braces, and parentheses
94
}
95
```
96
97
### Find and Replace
98
99
```groovy { .api }
100
class FindReplaceUtility {
101
// Find and replace functionality for the text editor
102
}
103
```
104
105
### Auto-Indentation
106
107
```groovy { .api }
108
class AutoIndentAction extends AbstractAction {
109
// Automatic indentation for Groovy code
110
}
111
```
112
113
## Usage Examples
114
115
### Basic Text Editor Setup
116
117
```groovy
118
def textEditor = new groovy.console.ui.ConsoleTextEditor()
119
textEditor.setShowLineNumbers(true)
120
textEditor.setEditable(true)
121
122
// Enable Groovy syntax highlighting
123
textEditor.enableHighLighter(groovy.console.ui.text.GroovyFilter.class)
124
```
125
126
### Accessing Underlying Editor
127
128
```groovy
129
def consoleEditor = new groovy.console.ui.ConsoleTextEditor()
130
def baseEditor = consoleEditor.getTextEditor()
131
132
// Configure the base editor
133
baseEditor.setFont(new Font("Consolas", Font.PLAIN, 14))
134
```
135
136
### Action Integration
137
138
```groovy
139
def textEditor = new groovy.console.ui.ConsoleTextEditor()
140
141
// Get editor actions for toolbar/menu integration
142
def undoAction = textEditor.getUndoAction()
143
def redoAction = textEditor.getRedoAction()
144
def printAction = textEditor.getPrintAction()
145
146
// Add actions to a toolbar
147
toolbar.add(undoAction)
148
toolbar.add(redoAction)
149
toolbar.add(printAction)
150
```
151
152
### Clipboard Operations
153
154
```groovy
155
def textEditor = new groovy.console.ui.ConsoleTextEditor()
156
157
// Check clipboard availability before operations
158
if (textEditor.clipBoardAvailable()) {
159
// Perform clipboard operations
160
textEditor.getTextEditor().copy()
161
textEditor.getTextEditor().paste()
162
}
163
```
164
165
## Syntax Highlighting Configuration
166
167
The text editor supports multiple levels of syntax highlighting:
168
169
### Groovy-Specific Highlighting
170
171
```groovy
172
// Enable Groovy syntax highlighting
173
textEditor.enableHighLighter(groovy.console.ui.text.GroovyFilter.class)
174
```
175
176
### Smart Document Features
177
178
The SmartDocumentFilter provides:
179
- Automatic bracket completion
180
- Smart indentation based on Groovy syntax
181
- Context-aware code formatting
182
- Real-time syntax validation
183
184
### Structured Syntax Handling
185
186
```groovy { .api }
187
class StructuredSyntaxHandler extends DefaultHandler {
188
// XML/structured content syntax handling
189
}
190
191
class StructuredSyntaxDocumentFilter extends DocumentFilter {
192
// Base structured syntax filtering
193
}
194
```
195
196
## Legacy API
197
198
The same text editing functionality is available in the legacy `groovy.ui.text` package:
199
200
```groovy
201
// Legacy imports (same API as groovy.console.ui.text)
202
import groovy.ui.text.TextEditor
203
import groovy.ui.text.GroovyFilter
204
import groovy.ui.text.SmartDocumentFilter
205
// ... etc
206
```
207
208
## Integration with Console
209
210
The text editor integrates seamlessly with the main Console:
211
- Automatic syntax highlighting for Groovy code
212
- Real-time error highlighting during compilation
213
- Context-sensitive auto-completion
214
- Integration with AST browser for code analysis
215
- Find/replace across console history