public class CodeView

  1. Object
  2. Component
  3. EditorView
  4. CodeView

ImplementsAnimation, Editable, StyleListener, TextInputClient

The pure code editor surface. It adds syntax highlighting (via an incremental Tokenizer), a line number gutter, bracket / quote auto close, tab expansion, auto indent, diagnostic squiggles and a code completion popup on top of the shared EditorView editing engine.

Constructors

public CodeView(EditorHost host)Creates a code editor view.

Methods

public void setLanguage(String language)Sets the highlighting language.
public String getLanguage()Returns the highlighting language id.
public void setTheme(String theme)Sets the color theme ("light" or "dark").
public void setShowLineNumbers(boolean show)Shows or hides the line number gutter.
public void setTabSize(int tabSize)Sets the number of spaces used for a tab / indentation step.
public void setDiagnostics(List<CodeDiagnostic> diagnostics)Sets the diagnostics to render.
public void setProtectedRegionMarkers(String startMarker, String endMarker)Protects every range delimited by matching marker strings from user edits.
protected void replaceRange(int start, int end, String text, boolean record)Replaces the range [start, end) with text, updating the caret, history and platform input state.
protected boolean isEditAllowed(int start, int end)Refuses edits that fall inside a protected region, whichever path they arrive through.
public void setCompletionEnabled(boolean enabled)Enables or disables the completion popup.
protected int getContentLeftInset()Returns the left inset (padding plus any gutter) at which text starts.
protected void paintBeforeContent(Graphics g, int originX, int originY, int firstLine, int lastLine, int lineHeight, Font f)Hook invoked before the visible lines are drawn, used by the code editor to paint the line number gutter.
protected void paintLine(Graphics g, int line, String text, int x, int y, int lineHeight, Font f)Paints a single line of text.
protected void documentEdited(int fromOffset)Hook invoked when the document is mutated at fromOffset, used by feature layers (e.g. the code editor) to invalidate cached tokenization from the affected line onward.
protected void paintLineDecorations(Graphics g, int line, String text, int x, int y, int lineHeight, Font f)Hook invoked after a line’s text is drawn, used by the code editor to paint diagnostic squiggles.
protected boolean handleTypedText(String text)Hook giving feature layers a chance to intercept typed text before the default insertion (used by the code editor for bracket / quote auto close, tab expansion and auto indent).
protected void handleTab(boolean dedent)Handles a tab key press (delivered as #KEY_TAB).
public void showCompletions(int reqId, List<CodeCompletion> items)Called by the backend with the provider’s proposals for a request.
public void pointerHover(int[] x, int[] y)Invoked for devices where the pointer can hover without actually clicking the display.
public void paint(Graphics g)This method paints the Component on the screen, it should be overriden by subclasses to perform custom drawing or invoke the UI API’s to let the PLAF perform the rendering.
public void pointerPressed(int x, int y)If this Component is focused, the pointer pressed event will call this method
public void keyPressed(int keyCode)If this Component is focused, the key pressed event will call this method
public void keyRepeated(int keyCode)If this Component is focused, the key repeat event will call this method.
public void keyReleased(int keyCode)If this Component is focused, the key released event will call this method
public void onKeyCommand(int command, int modifiers)Delivers a non text key command (navigation, deletion, clipboard, undo).
public void onEditorAction(int action)Delivers the keyboard return key action (done / next / search / send) configured through TextInputConfig.

Inherited fields

Inherited methods

From Component

setSameSize, isSetCursorSupported, parsePreferredSize, getDefaultDragTransparency, setDefaultDragTransparency, getEditingDelegate, setEditingDelegate, getCursor, setCursor, showNativeOverlay, hideNativeOverlay, updateNativeOverlay, getNativeOverlay, getAllStyles, getSameWidth, setSameWidth, getSameHeight, setSameHeight, initLaf, getUIManager, getX, setX, getOuterX, getInnerX, getY, setY, getOuterY, getInnerY, isVisible, setVisible, getClientProperty, stripMarginAndPadding, clearClientProperties, putClientProperty, getDirtyRegion, setDirtyRegion, isOpaque, setOpaque, getWidth, setWidth, getOuterWidth, getInnerWidth, getHeight, setHeight, getOuterHeight, getInnerHeight, isDragRegion, getDragRegionStatus, getBaseline, getBaselineResizeBehavior, getPreferredSizeStr, setPreferredSizeStr, getPreferredSize, setPreferredSize, getScrollDimension, calcScrollSize, setScrollSize, getPreferredW, setPreferredW, getPreferredH, setPreferredH, getOuterPreferredH, getInnerPreferredH, getOuterPreferredW, getInnerPreferredW, setSize, getUIID, setUIID, setUIIDFinal, setUIID, getInlineAllStyles, setInlineAllStyles, getInlineSelectedStyles, setInlineSelectedStyles, getInlineUnselectedStyles, setInlineUnselectedStyles, getInlineDisabledStyles, setInlineDisabledStyles, getInlinePressedStyles, setInlinePressedStyles, remove, getParent, getOwner, setOwner, isOwnedBy, containsOrOwns, addFocusListener, removeFocusListener, addScrollListener, removeScrollListener, fireClicked, isSelectableInteraction, getSelectCommandText, setSelectCommandText, getLabelForComponent, setLabelForComponent, paintBackgrounds, paintShadows, getAbsoluteX, getAbsoluteY, isInClippingRegion, paintIntersectingComponentsAbove, paintScrollbars, paintScrollbarX, getScrollOpacity, getSelectedRect, paintScrollbarY, paintComponent, paintComponent, getBorder, getScrollable, paintBackground, isScrollable, isScrollableX, isScrollableY, getScrollX, setScrollX, getScrollY, setScrollY, onScrollX, onScrollY, getDraggedx, getDraggedy, getBottomGap, getSideGap, contains, visibleBoundsContains, hasFixedPreferredSize, getBounds, getBounds, getVisibleBounds, getVisibleBounds, isFocusable, setFocusable, onSetFocusable, resetFocusable, getTabIndex, setTabIndex, getPreferredTabIndex, setPreferredTabIndex, isTraversable, setTraversable, setShouldCalcPreferredSize, handlesInput, setHandlesInput, hasFocus, setFocus, getComponentForm, getTopLevelContainer, repaint, repaint, longKeyPress, registerForAnimation, deregisterFromAnimation, getAnimationManager, getScrollAnimationSpeed, setScrollAnimationSpeed, isBlockLead, setBlockLead, isIgnorePointerEvents, setIgnorePointerEvents, isRippleEffect, setRippleEffect, getInlineStylesTheme, setInlineStylesTheme, shouldRenderComponentSelection, isHideInLandscape, setHideInLandscape, createStyleAnimation, isSmoothScrolling, setSmoothScrolling, stopScrollMomentum, pointerHoverReleased, pointerHoverPressed, pinch, pinchReleased, pinch, rotation, isPinchBlocksDragAndDrop, setPinchBlocksDragAndDrop, pointerDragged, getDragImage, getDragTransparency, setDragTransparency, toImage, dragInitiated, drawDraggedImage, draggingOver, dragEnter, dragExit, drop, addPullToRefresh, setPullToRefresh, respondsToPointerEvents, isStickyDrag, pointerPressed, isDragAndDropOperation, pointerReleased, setVerticalScrollBounds, setHorizontalScrollBounds, isVScrollThumbGrabbed, isHScrollThumbGrabbed, isVScrollThumbHover, isHScrollThumbHover, isTensileDragEnabled, setTensileDragEnabled, getTextSelectionSupport, addDropListener, removeDropListener, addDragOverListener, removeDragOverListener, isNativeDragSource, setNativeDragSource, getNativeDragOperation, setNativeDragOperation, createNativeDragOperation, isNativeDropTarget, setNativeDropTarget, getAcceptedDropMimeTypes, setAcceptedDropMimeTypes, getAcceptedDropActions, setAcceptedDropActions, canAcceptNativeDrop, nativeDragEnter, nativeDragOver, nativeDragExit, nativeDrop, addNativeDropListener, removeNativeDropListener, addNativeDragOverListener, removeNativeDragOverListener, dragFinished, addDragFinishedListener, addStateChangeListener, removeStateChangeListener, addPointerPressedListener, addLongPressListener, addContextMenuListener, removeContextMenuListener, addMouseWheelListener, removeMouseWheelListener, addStylusListener, removeStylusListener, mouseWheel, paintRippleOverlay, removePointerPressedListener, removeLongPressListener, removeDragFinishedListener, addPointerReleasedListener, removePointerReleasedListener, addPointerDraggedListener, removePointerDraggedListener, getDragSpeed, getStyle, getPressedStyle, setPressedStyle, initUnselectedStyle, initPressedStyle, initDisabledStyle, initSelectedStyle, getUnselectedStyle, setUnselectedStyle, getSelectedStyle, setSelectedStyle, getDisabledStyle, setDisabledStyle, installDefaultPainter, requestFocus, toString, paramString, refreshTheme, refreshTheme, refreshTheme, isDragActivated, getGridPosY, getGridPosX, scrollRectToVisible, scrollRectToVisible, paintBorder, paintBorderBackground, isCellRenderer, setCellRenderer, isScrollVisible, setScrollVisible, setIsScrollVisible, startEditingAsync, stopEditing, isEditing, isEditable, initComponent, isInitialized, setInitialized, styleChanged, getNextFocusDown, setNextFocusDown, getNextFocusUp, setNextFocusUp, getNextFocusLeft, setNextFocusLeft, getNextFocusRight, setNextFocusRight, isEnabled, setEnabled, getName, setName, initCustomStyle, deinitializeCustomStyle, isRTL, setRTL, isTactileTouch, isTactileTouch, setTactileTouch, getPropertyNames, getPropertyTypes, getPropertyTypeNames, getPropertyValue, setPropertyValue, paintLockRelease, paintLock, isSnapToGrid, setSnapToGrid, shouldBlockSideSwipe, shouldBlockSideSwipeLeft, shouldBlockSideSwipeRight, blocksSideSwipe, isFlatten, setFlatten, getTensileLength, setTensileLength, isGrabsPointerEvents, setGrabsPointerEvents, getScrollOpacityChangeSpeed, setScrollOpacityChangeSpeed, growShrink, isAlwaysTensile, setAlwaysTensile, isDraggable, setDraggable, isDropTarget, setDropTarget, isChildOf, isHideInPortrait, setHideInPortrait, cancelRepaints, getBindablePropertyNames, getBindablePropertyTypes, bindProperty, unbindProperty, getBoundPropertyValue, setBoundPropertyValue, getCloudBoundProperty, setCloudBoundProperty, getCloudDestinationProperty, setCloudDestinationProperty, getComponentState, setComponentState, setHidden, isHidden, setHidden, isHidden, announceForAccessibility, getAccessibilityText, setAccessibilityText, getSemantics, getAccessibilityNode, accessibilityChanged, accessibilityChanged, getTooltip, setTooltip

Constructor details

CodeView

public CodeView(EditorHost host)
Creates a code editor view.

Method details

setLanguage

public void setLanguage(String language)
Sets the highlighting language.

getLanguage

public String getLanguage()
Returns the highlighting language id.

setTheme

public void setTheme(String theme)
Sets the color theme ("light" or "dark").

setShowLineNumbers

public void setShowLineNumbers(boolean show)
Shows or hides the line number gutter.

setTabSize

public void setTabSize(int tabSize)
Sets the number of spaces used for a tab / indentation step.

setDiagnostics

public void setDiagnostics(List<CodeDiagnostic> diagnostics)
Sets the diagnostics to render.

setProtectedRegionMarkers

public void setProtectedRegionMarkers(String startMarker, String endMarker)
Protects every range delimited by matching marker strings from user edits. Passing null or an empty marker clears the protection. Whole-document replacement remains available so generated source can still be refreshed by the host.

replaceRange

protected void replaceRange(int start, int end, String text, boolean record)
Replaces the range [start, end) with text, updating the caret, history and platform input state.

Parameters

start int
inclusive start offset
end int
exclusive end offset
text String
the replacement text (may be empty)
record boolean
true to record the mutation for undo

isEditAllowed

protected boolean isEditAllowed(int start, int end)
Refuses edits that fall inside a protected region, whichever path they arrive through. Input method composition writes to the document directly rather than through #replaceRange(int, int, String, boolean), so checking only there let a composing keyboard insert provisional text into generated source that is meant to be read only.

Parameters

start int
inclusive start offset of the edit
end int

exclusive end offset of the edit

Returns: true when the edit may proceed

setCompletionEnabled

public void setCompletionEnabled(boolean enabled)
Enables or disables the completion popup.

getContentLeftInset

protected int getContentLeftInset()
Returns the left inset (padding plus any gutter) at which text starts.

paintBeforeContent

protected void paintBeforeContent(Graphics g, int originX, int originY, int firstLine, int lastLine, int lineHeight, Font f)
Hook invoked before the visible lines are drawn, used by the code editor to paint the line number gutter. originY is scroll adjusted so line n sits at originY + n * lineHeight.

paintLine

protected void paintLine(Graphics g, int line, String text, int x, int y, int lineHeight, Font f)
Paints a single line of text. Subclasses override to add syntax coloring; the base implementation draws the whole line in the default text color.

Parameters

g Graphics
the graphics context
line int
the zero based line index
text String
the line text
x int
the x pixel at which the line text starts
y int
the top y pixel of the line
lineHeight int
the line height
f Font
the font

documentEdited

protected void documentEdited(int fromOffset)
Hook invoked when the document is mutated at fromOffset, used by feature layers (e.g. the code editor) to invalidate cached tokenization from the affected line onward.

paintLineDecorations

protected void paintLineDecorations(Graphics g, int line, String text, int x, int y, int lineHeight, Font f)
Hook invoked after a line’s text is drawn, used by the code editor to paint diagnostic squiggles.

handleTypedText

protected boolean handleTypedText(String text)
Hook giving feature layers a chance to intercept typed text before the default insertion (used by the code editor for bracket / quote auto close, tab expansion and auto indent). Return true when the text was consumed. The default returns false.

Parameters

text String
the text being typed / committed

handleTab

protected void handleTab(boolean dedent)
Handles a tab key press (delivered as #KEY_TAB). Feature layers override this for indent / dedent behavior. The default inserts a tab character on a plain tab and does nothing on a dedent (a plain editor has no indentation to remove).

Parameters

dedent boolean
true when shift was held (shift-tab)

showCompletions

public void showCompletions(int reqId, List<CodeCompletion> items)
Called by the backend with the provider’s proposals for a request.

Parameters

reqId int
the request sequence id
items List<CodeCompletion>
the proposals

pointerHover

public void pointerHover(int[] x, int[] y)
Invoked for devices where the pointer can hover without actually clicking the display. This is true for PC mouse pointer as well as some devices such as the BB storm.

Parameters

x int[]
the pointer x coordinate
y int[]
the pointer y coordinate

paint

public void paint(Graphics g)
This method paints the Component on the screen, it should be overriden by subclasses to perform custom drawing or invoke the UI API’s to let the PLAF perform the rendering.

Parameters

g Graphics
the component graphics

pointerPressed

public void pointerPressed(int x, int y)
If this Component is focused, the pointer pressed event will call this method

Parameters

x int
the pointer x coordinate
y int
the pointer y coordinate

keyPressed

public void keyPressed(int keyCode)
If this Component is focused, the key pressed event will call this method

Parameters

keyCode int
the key code value to indicate a physical key.

keyRepeated

public void keyRepeated(int keyCode)
If this Component is focused, the key repeat event will call this method.

Parameters

keyCode int
the key code value to indicate a physical key.

keyReleased

public void keyReleased(int keyCode)
If this Component is focused, the key released event will call this method

Parameters

keyCode int
the key code value to indicate a physical key.

onKeyCommand

public void onKeyCommand(int command, int modifiers)
Delivers a non text key command (navigation, deletion, clipboard, undo). Text producing keys are delivered through #commitText instead.

Parameters

command int
one of the KEY_* constants
modifiers int
a bit mask of the MOD_* constants

onEditorAction

public void onEditorAction(int action)
Delivers the keyboard return key action (done / next / search / send) configured through TextInputConfig.

Parameters

action int
one of the TextInputConfig ACTION_* constants