codeditor

package module
v0.0.0-...-7d244e9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 1, 2025 License: MIT Imports: 12 Imported by: 0

README

codeditor

A powerful, customizable code editor widget for Fyne applications with syntax highlighting and VS Code-style theme support.

Features

  • Syntax Highlighting: Support for multiple programming languages including Go, Python, JavaScript, Java, C/C++
  • VS Code-Style Themes: Built-in themes (Dark, Light, VS Code Dark, Monokai) with JSON theme loading support
  • Customizable: Font size, tab size, line numbers, and more
  • Keyboard Navigation: Full cursor movement, text editing, and selection support
  • Extensible: Easy to add new languages and themes

Installation

go get github.com/ispapp/psshclient/pkg/codeditor

Quick Start

package main

import (
    "fyne.io/fyne/v2/app"
    editor "github.com/ispapp/psshclient/pkg/codeditor"
)

func main() {
    myApp := app.New()
    myWindow := myApp.NewWindow("Code Editor")
    
    // Create a new code editor
    codeEditor := editor.NewScriptEditor()
    codeEditor.SetLanguage("go")
    codeEditor.SetContent(`package main

import "fmt"

func main() {
    fmt.Println("Hello, World!")
}`)
    
    myWindow.SetContent(codeEditor)
    myWindow.ShowAndRun()
}

API Reference

Creating an Editor
// Create a new editor with default settings
editor := NewScriptEditor()

// Create with specific language and theme
editor := CreateCodeEditor("python", GetVSCodeDarkTheme())

// Create with initial content
editor := CreateCodeEditorWithContent(code, "go", GetDefaultTheme())
Content Management
// Set and get content
editor.SetContent("your code here")
content := editor.GetContent()

// Listen for changes
editor.SetOnChanged(func(content string) {
    // Handle content changes
    fmt.Println("Content changed:", len(content), "characters")
})
Language Support
// Set programming language for syntax highlighting
editor.SetLanguage("go")        // Go
editor.SetLanguage("python")    // Python
editor.SetLanguage("javascript") // JavaScript
editor.SetLanguage("java")      // Java
editor.SetLanguage("c")         // C
editor.SetLanguage("cpp")       // C++

// Get current language
language := editor.GetLanguage()
Theme Customization
// Use built-in themes
editor.SetTheme(GetDefaultTheme())    // Dark theme
editor.SetTheme(GetLightTheme())      // Light theme
editor.SetTheme(GetVSCodeDarkTheme()) // VS Code Dark
editor.SetTheme(GetMonokaiTheme())    // Monokai

// Load theme from JSON file
theme, err := LoadThemeFromJSON("my-theme.json")
if err == nil {
    editor.SetTheme(theme)
}
Appearance Settings
// Font size
editor.SetFontSize(14.0)
size := editor.GetFontSize()

// Tab size
editor.SetTabSize(4)
tabSize := editor.GetTabSize()

// Line numbers
editor.SetShowLineNumbers(true)
showLines := editor.GetShowLineNumbers()

Theme Format

Themes can be defined in JSON format:

{
  "name": "My Custom Theme",
  "background": "#1e1e1e",
  "foreground": "#d4d4d4",
  "tokenColors": {
    "keyword": "#569cd6",
    "string": "#ce9178",
    "comment": "#6a9955",
    "number": "#b5cea8",
    "operator": "#d4d4d4",
    "identifier": "#9cdcfe",
    "function": "#dcdcaa",
    "type": "#4ec9b0",
    "plain": "#d4d4d4"
  }
}
Saving Themes
// Save a theme to JSON file
theme := GetMonokaiTheme()
err := SaveThemeToJSON(theme, "monokai-theme.json")

Supported Languages

  • Go: Full syntax highlighting with keywords, types, functions, and more
  • Python: Support for Python 3 syntax including decorators, f-strings, and async/await
  • JavaScript: ES6+ support with classes, arrow functions, template literals
  • Java: Complete Java syntax highlighting
  • C/C++: Support for both C and C++ with preprocessor directives
  • Generic: Basic highlighting for unknown languages

Keyboard Controls

  • Arrow Keys: Navigate cursor
  • Home/End: Move to line start/end
  • Backspace/Delete: Delete characters
  • Tab: Insert spaces (configurable tab size)
  • Enter: Insert new line

Examples

See the example.go file for a complete demonstration including:

  • Theme switching
  • Language selection
  • Font size adjustment
  • Line number toggle
  • Different code samples

Extending the Editor

Adding New Languages
  1. Add language-specific rules in lexer.go
  2. Create a new initialize[Language]Rules() method
  3. Add the language to the switch statement in initializeRules()
Creating Custom Themes
  1. Create a new Theme struct with your colors
  2. Use SaveThemeToJSON() to export as JSON
  3. Load with LoadThemeFromJSON() for reuse

Architecture

The widget follows Fyne's architecture patterns:

  • ScriptEditor: Main widget implementing fyne.Widget
  • scriptEditorRenderer: Custom renderer implementing fyne.WidgetRenderer
  • Lexer: Tokenizes code for syntax highlighting
  • Theme: Defines colors for different token types

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Add tests for new functionality
  4. Submit a pull request

License

This project is licensed under the same license as the parent project.

Documentation

Overview

Package FyneCodeEditorWidget provides a powerful, customizable code editor widget for Fyne applications.

This package implements a script editor with syntax highlighting, VS Code-style themes, and comprehensive editing capabilities. It supports multiple programming languages and allows for extensive customization of appearance and behavior.

Key Features:

  • Syntax highlighting for Go, Python, JavaScript, Java, C/C++, and more
  • VS Code-style color themes with JSON support
  • Customizable font size, tab size, and line numbers
  • Full keyboard navigation and text editing
  • Extensible architecture for adding new languages and themes

Basic Usage:

editor := FyneCodeEditorWidget.NewScriptEditor()
editor.SetLanguage("go")
editor.SetTheme(FyneCodeEditorWidget.GetDefaultTheme())
editor.SetContent("package main\n\nfunc main() {\n\t// Your code here\n}")

The widget follows Fyne's standard architecture with separate Widget and WidgetRenderer interfaces. The lexer provides tokenization for syntax highlighting, while the theme system allows for comprehensive color customization.

For complete examples and documentation, see the README.md file and example.go.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Example

func Example()

Example demonstrates how to use the FyneCodeEditorWidget

func GetBuiltinThemes

func GetBuiltinThemes() map[string]*Theme

GetBuiltinThemes returns all built-in themes

func SaveThemeToJSON

func SaveThemeToJSON(theme *Theme, filepath string) error

SaveThemeToJSON saves a theme to a JSON file

Types

type Lexer

type Lexer struct {
	// contains filtered or unexported fields
}

Lexer handles tokenization of source code for syntax highlighting

func NewLexer

func NewLexer(language string) *Lexer

NewLexer creates a new lexer for the specified language

func (*Lexer) Tokenize

func (l *Lexer) Tokenize(content string) []Token

Tokenize breaks down the input text into tokens based on the language rules

type LexerRule

type LexerRule struct {
	Pattern   *regexp.Regexp
	TokenType TokenType
	Priority  int // Higher priority rules are checked first
}

LexerRule defines a pattern and token type for lexical analysis

type ScriptEditor

type ScriptEditor struct {
	widget.BaseWidget
	// contains filtered or unexported fields
}

ScriptEditor is a custom Fyne widget for code editing with syntax highlighting

func CreateCodeEditor

func CreateCodeEditor(language string, theme *Theme) *ScriptEditor

CreateCodeEditor is a convenience function to create a new code editor with default settings

func CreateCodeEditorWithContent

func CreateCodeEditorWithContent(content, language string, theme *Theme) *ScriptEditor

CreateCodeEditorWithContent creates a new code editor with initial content

func NewScriptEditor

func NewScriptEditor() *ScriptEditor

NewScriptEditor creates a new script editor widget

func (*ScriptEditor) CreateRenderer

func (e *ScriptEditor) CreateRenderer() fyne.WidgetRenderer

CreateRenderer creates the custom renderer for the script editor

func (*ScriptEditor) Focusable

func (e *ScriptEditor) Focusable() bool

Focusable makes the editor focusable

func (*ScriptEditor) GetContent

func (e *ScriptEditor) GetContent() string

GetContent returns the current content of the editor

func (*ScriptEditor) GetFontSize

func (e *ScriptEditor) GetFontSize() float32

GetFontSize returns the current font size

func (*ScriptEditor) GetLanguage

func (e *ScriptEditor) GetLanguage() string

GetLanguage returns the current programming language

func (*ScriptEditor) GetShowLineNumbers

func (e *ScriptEditor) GetShowLineNumbers() bool

GetShowLineNumbers returns whether line numbers are shown

func (*ScriptEditor) GetTabSize

func (e *ScriptEditor) GetTabSize() int

GetTabSize returns the current tab size

func (*ScriptEditor) GetTheme

func (e *ScriptEditor) GetTheme() *Theme

GetTheme returns the current theme

func (*ScriptEditor) SetContent

func (e *ScriptEditor) SetContent(content string)

SetContent sets the content of the editor

func (*ScriptEditor) SetFontSize

func (e *ScriptEditor) SetFontSize(size float32)

SetFontSize sets the font size for the editor

func (*ScriptEditor) SetLanguage

func (e *ScriptEditor) SetLanguage(language string)

SetLanguage sets the programming language for syntax highlighting

func (*ScriptEditor) SetOnChanged

func (e *ScriptEditor) SetOnChanged(callback func(string))

SetOnChanged sets the callback function for content changes

func (*ScriptEditor) SetShowLineNumbers

func (e *ScriptEditor) SetShowLineNumbers(show bool)

SetShowLineNumbers enables or disables line number display

func (*ScriptEditor) SetTabSize

func (e *ScriptEditor) SetTabSize(size int)

SetTabSize sets the tab size for indentation

func (*ScriptEditor) SetTheme

func (e *ScriptEditor) SetTheme(theme *Theme)

SetTheme sets the color theme for the editor

func (*ScriptEditor) TypedKey

func (e *ScriptEditor) TypedKey(key *fyne.KeyEvent)

TypedKey handles special key presses

func (*ScriptEditor) TypedRune

func (e *ScriptEditor) TypedRune(r rune)

TypedRune handles typed characters

type Theme

type Theme struct {
	Name        string
	Background  color.Color
	Foreground  color.Color
	TokenColors map[TokenType]color.Color
}

Theme represents a color theme for syntax highlighting

func GetDefaultTheme

func GetDefaultTheme() *Theme

GetDefaultTheme returns the default dark theme for the editor

func GetLightTheme

func GetLightTheme() *Theme

GetLightTheme returns a light theme for the editor

func GetMonokaiTheme

func GetMonokaiTheme() *Theme

GetMonokaiTheme returns a Monokai theme

func GetVSCodeDarkTheme

func GetVSCodeDarkTheme() *Theme

GetVSCodeDarkTheme returns a VS Code dark theme

func LoadThemeFromJSON

func LoadThemeFromJSON(filepath string) (*Theme, error)

LoadThemeFromJSON loads a theme from a JSON file

type ThemeDefinition

type ThemeDefinition struct {
	Name        string            `json:"name"`
	Background  string            `json:"background"`
	Foreground  string            `json:"foreground"`
	TokenColors map[string]string `json:"tokenColors"`
}

ThemeDefinition represents a theme definition for JSON loading

type Token

type Token struct {
	Type  TokenType
	Value string
	Start int
	End   int
}

Token represents a syntax token with its type and value

type TokenType

type TokenType string

TokenType represents different types of syntax tokens

const (
	TokenKeyword    TokenType = "keyword"
	TokenString     TokenType = "string"
	TokenComment    TokenType = "comment"
	TokenNumber     TokenType = "number"
	TokenOperator   TokenType = "operator"
	TokenIdentifier TokenType = "identifier"
	TokenFunction   TokenType = "function"
	TokenType_      TokenType = "type"
	TokenPlain      TokenType = "plain"
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL