Class CodeEditor

All Implemented Interfaces:
Animation, Editable, EditorHost, StyleListener, Iterable<Component>

public class CodeEditor extends AbstractEditorComponent

An IDE style source code editor with syntax highlighting, a line-number gutter and asynchronous code completion.

CodeEditor is built for editing source code on both touch devices (with the virtual keyboard) and desktops (with a physical keyboard). Code completion is driven by a CodeCompletionProvider, which can resolve proposals locally or from a remote language server.

Like RichTextArea, ports with low-level text input use the pure Codename One text engine (com.codename1.ui.editor) with a built-in incremental syntax highlighter. Ports without that input contract use an editable BrowserComponent fallback, and a port can transparently supply a native code editor instead.

Example
CodeEditor editor = new CodeEditor();
editor.setLanguage("java");
editor.setText("public class Main {\n\n}");
editor.setCompletionProvider((ed, code, cursor, results) -> {
    List<CodeCompletion> out = new ArrayList<>();
    out.add(new CodeCompletion("System.out.println(", "System.out.println()").setType("method"));
    results.onSucess(out);
});
form.add(BorderLayout.CENTER, editor);
  • Constructor Details

    • CodeEditor

      public CodeEditor()
      Creates an empty code editor.
    • CodeEditor

      public CodeEditor(String language, String text)

      Creates a code editor initialized with the supplied source and language.

      Parameters
      • language: the language id used for syntax highlighting (e.g. "java", "javascript", "kotlin", "css", "xml", "json", "python")

      • text: the initial source code

  • Method Details

    • setText

      public void setText(String text)

      Replaces the entire editor content.

      Parameters
      • text: the source code
    • getText

      public void getText(SuccessCallback<String> callback)

      Retrieves the current source code. The callback is invoked on the EDT.

      Parameters
      • callback: receives the source code
    • setLanguage

      public void setLanguage(String language)

      Sets the language used for syntax highlighting.

      Parameters
      • language: the language id (e.g. "java", "javascript", "kotlin", "css", "xml", "json", "python")
    • getLanguage

      public String getLanguage()
      Returns the current highlighting language id.
    • setTheme

      public void setTheme(String theme)

      Sets the color theme. Currently "light" and "dark" are supported.

      Parameters
      • theme: the theme id
    • getTheme

      public String getTheme()
      Returns the current theme id.
    • setShowLineNumbers

      public void setShowLineNumbers(boolean show)

      Shows or hides the line-number gutter.

      Parameters
      • show: true to show line numbers
    • isShowLineNumbers

      public boolean isShowLineNumbers()
      Returns true if the line-number gutter is shown.
    • setTabSize

      public void setTabSize(int tabSize)

      Sets the number of spaces inserted for a tab / used for indentation.

      Parameters
      • tabSize: the indentation width in spaces
    • getTabSize

      public int getTabSize()
      Returns the indentation width in spaces.
    • setReadOnly

      public void setReadOnly(boolean readOnly)

      Makes the editor read-only or editable. This is a convenience around AbstractEditorComponent#setEditable(boolean).

      Parameters
      • readOnly: true to prevent editing
    • isReadOnly

      public boolean isReadOnly()
      Returns true when the editor is read-only.
    • insertAtCursor

      public void insertAtCursor(String text)

      Inserts text at the current caret position, replacing any active selection.

      Parameters
      • text: the text to insert
    • getCursorPosition

      public void getCursorPosition(SuccessCallback<Integer> callback)

      Retrieves the current caret character offset. The callback is invoked on the EDT.

      Parameters
      • callback: receives the caret offset as an Integer
    • setDiagnostics

      public void setDiagnostics(List<CodeDiagnostic> diagnostics)

      Sets the diagnostics (errors / warnings / hints) displayed in the editor as squiggly underlines, gutter markers and tooltips. Pass an empty list (or null) to clear all diagnostics.

      Parameters
      • diagnostics: the diagnostics to display
    • setEngineURL

      public void setEngineURL(String url)

      Points the browser fallback at a custom editor page in the app hierarchy. Ports with low-level text input continue to use the pure editor; this URL is used only where the browser fallback is required.

      Parameters
      • url: an app-hierarchy URL, or null for the built-in page
    • getEngineURL

      public String getEngineURL()
      Returns the custom browser-engine URL, or null for the built-in page.
    • setCompletionProvider

      public void setCompletionProvider(CodeCompletionProvider provider)

      Sets the provider that supplies code completion proposals. Passing null disables completion.

      Parameters
      • provider: the completion provider, or null to disable completion
    • getCompletionProvider

      public CodeCompletionProvider getCompletionProvider()
      Returns the current completion provider, or null if none is set.