@fsegurai/codemirror-theme-forest
Version:
Forest theme for the CodeMirror code editor
500 lines (493 loc) • 18.2 kB
JavaScript
'use strict';
var view = require('@codemirror/view');
var language = require('@codemirror/language');
var highlight = require('@lezer/highlight');
// Helper module for styling options
const generalContent = {
fontSize: '14px',
fontFamily: 'JetBrains Mono, Consolas, monospace',
lineHeight: '1.6',
};
const generalCursor = {
borderLeftWidth: '2px',
};
const generalDiff = {
insertedTextDecoration: 'none',
deletedTextDecoration: 'line-through',
insertedLinePadding: '1px 3px',
borderRadious: '3px'};
const generalGutter = {
border: 'none',
paddingRight: '8px',
fontSize: '0.9em',
fontWeight: '500',
};
const generalPanel = {
border: 'none',
borderRadius: '4px',
padding: '2px 10px',
};
const generalLine = {
borderRadius: '2px',
};
const generalMatching = {
borderRadius: '2px',
};
const generalPlaceholder = {
borderRadius: '4px',
padding: '0 5px',
margin: '0 2px',
};
const generalScroller = {
width: '12px',
height: '12px',
borderRadius: '6px',
};
const generalSearchField = {
borderRadius: '4px',
padding: '2px 6px',
};
const generalTooltip = {
borderRadius: '4px',
borderRadiusSelected: '3px',
lineHeight: '1.3',
padding: '4px 8px',
paddingRight: '8px',
};
/**
* Function to apply merge revert styles for a theme
* @param styles Styles for the merge revert buttons
* @param styles.backgroundColor Background color of the revert area
* @param styles.borderColor Border color of the revert area
* @param styles.buttonColor Color of the revert buttons
* @param styles.buttonHoverColor Hover color of the revert buttons
*/
function applyMergeRevertStyles(styles) {
// Create a stylesheet
const styleEl = document.createElement('style');
styleEl.id = 'cm-merge-revert-styles';
// Define CSS with the theme-specific values
styleEl.textContent = `
.cm-merge-revert {
display: flex;
flex-direction: column;
align-items: center;
justify-content: flex-start;
padding: 4px;
background-color: ${styles.backgroundColor};
border-left: 1px solid ${styles.borderColor};
border-right: 1px solid ${styles.borderColor};
width: 32px;
}
.cm-merge-revert button {
width: 100%;
height: auto;
background-color: transparent;
border: none;
color: ${styles.buttonColor};
cursor: pointer;
margin: 0 auto;
font-size: 20px;
}
.cm-merge-revert button:hover {
background-color: ${styles.buttonHoverColor};
}
`;
// Remove any existing merge styles
const existingStyle = document.getElementById('cm-merge-revert-styles');
if (existingStyle)
existingStyle.remove();
// Add the new styles
document.head.appendChild(styleEl);
}
/**
* Enhanced Forest theme color palette
* -----------------------------------
* Colors organized by natural forest elements with visual color blocks for reference
*/
// Core natural elements
const base00 = '#1a2a33'; // Background - deeper forest shade for better contrast
const base01 = '#d0d8e0'; // Foreground - clearer moonlight for better readability
// const base02 = '#2d3c44'; // Selection - dark forest floor, more defined
const base03 = '#607d8b'; // Comments - silvery bark gray, more legible
// Flora elements (accent colors)
const base04 = '#c594c5'; // Cursor - forest wildflower
const base05 = '#a9d3ab'; // Keywords - lighter, fresher leaf green
const base06 = '#4db6ac'; // Variables - clearer water reflection
const base07 = '#78aadc'; // Functions - brighter dusk sky
const base08 = '#ef5350'; // Strings - vibrant woodland berries
const base09 = '#bc8f6a'; // Numbers - rich forest soil
const base0A = '#ffb74d'; // Classes - golden sunlight through canopy
const base0B = '#9ccc65'; // Properties - fresh moss
const base0C = '#4dd0e1'; // Special chars - forest stream
const base0D = '#7986cb'; // Tags - morning mist
const base0E = '#ba68c8'; // Operators - purple wildflowers
// const base0F = '#f57f17'; // Metadata - autumn leaves
// UI-specific colors
const invalid = '#ff5252'; // Error highlight - warning red berry
const darkBackground = '#233039'; // Panel background - shadowy forest
const highlightBackground = '#314443aa'; // Active line - forest clearing with dappled light
const tooltipBackground = '#2a3b42'; // Tooltip background - darker canopy shadow
const cursor = base04; // Cursor color
const selection = '#2c5a3acc'; // Selection - translucent forest green
const selectionMatch = '#5d482faa'; // Selection match - amber forest light
const lineNumbers = '#607d8b90'; // Line numbers - faded tree bark
const activeBracketBg = '#2c5a3a80'; // Active bracket - forest green
const activeBracketBorder = base0A; // Active bracket border - golden sunlight
// Diff/merge specific colors
const addedBackground = '#28513a80', // Forest green with transparency for insertions
removedBackground = '#763c3c70', // Deep red with transparency for deletions
addedText = '#9ccc65', // Moss green for added text
removedText = '#ef5350'; // Berry red for removed text
/**
* Enhanced editor theme styles for Forest
*/
const forestTheme = view.EditorView.theme({
// Base editor styles
'&': {
color: base01,
backgroundColor: base00,
fontSize: generalContent.fontSize,
fontFamily: generalContent.fontFamily,
},
// Content and cursor
'.cm-content': {
caretColor: cursor,
lineHeight: generalContent.lineHeight,
},
'.cm-cursor, .cm-dropCursor': {
borderLeftColor: cursor,
borderLeftWidth: generalCursor.borderLeftWidth,
},
'.cm-fat-cursor': {
backgroundColor: `${cursor}99`,
color: base00,
},
// Selection
'&.cm-focused > .cm-scroller > .cm-selectionLayer .cm-selectionBackground, .cm-selectionBackground, .cm-content ::selection': {
backgroundColor: selection,
color: base01,
},
// Make sure selection appears above active line
'.cm-selectionLayer': {
zIndex: 100,
},
// Search functionality
'.cm-searchMatch': {
backgroundColor: '#4d653b80',
outline: `1px solid ${base0A}90`,
color: base01,
borderRadius: generalSearchField.borderRadius,
'& span': {
color: base01,
},
},
'.cm-searchMatch.cm-searchMatch-selected': {
backgroundColor: '#42855c',
color: base01,
padding: generalSearchField.padding,
'& span': {
color: base01,
},
},
'.cm-search.cm-panel.cm-textfield': {
color: base01,
borderRadius: generalSearchField.borderRadius,
padding: generalSearchField.padding,
},
// Panels
'.cm-panels': {
backgroundColor: darkBackground,
color: base01,
borderRadius: '0 0 4px 4px',
},
'.cm-panels.cm-panels-top': {
borderBottom: '1px solid #3e5059',
},
'.cm-panels.cm-panels-bottom': {
borderTop: '1px solid #3e5059',
},
'.cm-panel button': {
backgroundColor: darkBackground,
color: base01,
border: generalPanel.border,
borderRadius: generalPanel.borderRadius,
padding: generalPanel.padding,
},
'.cm-panel button:hover': {
backgroundColor: '#2d3d47',
},
// Line highlighting
'.cm-activeLine': {
backgroundColor: highlightBackground,
borderRadius: generalLine.borderRadius,
zIndex: 1,
},
// Gutters
'.cm-gutters': {
backgroundColor: darkBackground,
color: lineNumbers,
border: generalGutter.border,
borderRight: '1px solid #3e505980',
paddingRight: generalGutter.paddingRight,
},
'.cm-activeLineGutter': {
backgroundColor: highlightBackground,
color: base01,
fontWeight: generalGutter.fontWeight,
},
'.cm-lineNumbers': {
fontSize: generalGutter.fontSize,
},
'.cm-foldGutter': {
fontSize: generalGutter.fontSize,
},
'.cm-foldGutter .cm-gutterElement': {
color: lineNumbers,
cursor: 'pointer',
},
'.cm-foldGutter .cm-gutterElement:hover': {
color: base01,
},
// Diff/Merge View Styles
// Inserted/Added Content
'.cm-insertedLine': {
textDecoration: generalDiff.insertedTextDecoration,
backgroundColor: addedBackground,
color: addedText,
padding: generalDiff.insertedLinePadding,
borderRadius: generalDiff.borderRadious,
},
'ins.cm-insertedLine, ins.cm-insertedLine:not(:has(.cm-changedText))': {
textDecoration: generalDiff.insertedTextDecoration,
backgroundColor: `${addedBackground} !important`,
color: addedText,
padding: generalDiff.insertedLinePadding,
borderRadius: generalDiff.borderRadious,
border: `1px solid ${addedText}40`,
},
'ins.cm-insertedLine .cm-changedText': {
background: 'transparent !important',
},
// Deleted/Removed Content
'.cm-deletedLine': {
textDecoration: generalDiff.deletedTextDecoration,
backgroundColor: removedBackground,
color: removedText,
padding: generalDiff.insertedLinePadding,
borderRadius: generalDiff.borderRadious,
},
'del.cm-deletedLine, del, del:not(:has(.cm-deletedText))': {
textDecoration: generalDiff.deletedTextDecoration,
backgroundColor: `${removedBackground} !important`,
color: removedText,
padding: generalDiff.insertedLinePadding,
borderRadius: generalDiff.borderRadious,
border: `1px solid ${removedText}40`,
},
'del .cm-deletedText, del .cm-changedText': {
background: 'transparent !important',
},
// Tooltips and autocomplete
'.cm-tooltip': {
backgroundColor: tooltipBackground,
border: '1px solid #3e5059',
borderRadius: generalTooltip.borderRadius,
padding: generalTooltip.padding,
boxShadow: '0 2px 8px rgba(0, 0, 0, 0.3)',
},
'.cm-tooltip-autocomplete': {
'& > ul': {
backgroundColor: tooltipBackground,
border: 'none',
},
'& > ul > li': {
padding: generalTooltip.padding,
lineHeight: generalTooltip.lineHeight,
},
'& > ul > li[aria-selected]': {
backgroundColor: selection,
color: base01,
borderRadius: generalTooltip.borderRadiusSelected,
},
'& > ul > li > span.cm-completionIcon': {
color: base03,
paddingRight: generalTooltip.paddingRight,
},
'& > ul > li > span.cm-completionDetail': {
color: base03,
fontStyle: 'italic',
},
},
'.cm-tooltip .cm-tooltip-arrow:before': {
borderTopColor: 'transparent',
borderBottomColor: 'transparent',
},
'.cm-tooltip .cm-tooltip-arrow:after': {
borderTopColor: tooltipBackground,
borderBottomColor: tooltipBackground,
},
// Diagnostics styling
'.cm-diagnostic': {
'&-error': {
borderLeft: `3px solid ${invalid}`,
},
'&-warning': {
borderLeft: `3px solid ${base0A}`,
},
'&-info': {
borderLeft: `3px solid ${base0C}`,
},
},
'.cm-lintPoint-error': {
borderBottom: `2px wavy ${invalid}`,
},
'.cm-lintPoint-warning': {
borderBottom: `2px wavy ${base0A}`,
},
// Matching brackets
'.cm-matchingBracket': {
backgroundColor: activeBracketBg,
outline: `1px solid ${activeBracketBorder}80`,
borderRadius: generalMatching.borderRadius,
},
'.cm-nonmatchingBracket': {
backgroundColor: '#5d482f80',
outline: `1px solid ${invalid}80`,
borderRadius: generalMatching.borderRadius,
},
// Selection matches
'.cm-selectionMatch': {
backgroundColor: selectionMatch,
outline: `1px solid ${base0A}40`,
borderRadius: generalMatching.borderRadius,
},
// Fold placeholder
'.cm-foldPlaceholder': {
backgroundColor: 'transparent',
color: base03,
border: `1px dotted ${base0A}70`,
borderRadius: generalPlaceholder.borderRadius,
padding: generalPlaceholder.padding,
margin: generalPlaceholder.margin,
},
// Focus outline
'&.cm-focused': {
outline: 'none',
boxShadow: `0 0 0 2px ${base00}, 0 0 0 3px ${base0A}40`,
},
// Scrollbars
'& .cm-scroller::-webkit-scrollbar': {
width: generalScroller.width,
height: generalScroller.height,
},
'& .cm-scroller::-webkit-scrollbar-track': {
background: darkBackground,
},
'& .cm-scroller::-webkit-scrollbar-thumb': {
backgroundColor: '#3e5059',
borderRadius: generalScroller.borderRadius,
border: `3px solid ${darkBackground}`,
},
'& .cm-scroller::-webkit-scrollbar-thumb:hover': {
backgroundColor: '#4e616a',
},
// Ghost text
'.cm-ghostText': {
opacity: '0.5',
color: '#8fa7b5',
},
}, { dark: true });
/**
* Enhanced syntax highlighting for Forest theme
*/
const forestHighlightStyle = language.HighlightStyle.define([
// Keywords and language constructs
{ tag: highlight.tags.keyword, color: base05, fontWeight: 'bold' },
{ tag: highlight.tags.controlKeyword, color: base05, fontWeight: 'bold' },
{ tag: highlight.tags.moduleKeyword, color: base05, fontWeight: 'bold' },
// Names and variables
{ tag: [highlight.tags.name, highlight.tags.deleted, highlight.tags.character, highlight.tags.macroName], color: base06 },
{ tag: [highlight.tags.variableName], color: base06 },
{ tag: [highlight.tags.propertyName], color: base0B, fontStyle: 'normal' },
// Classes and types
{ tag: [highlight.tags.typeName], color: base0A },
{ tag: [highlight.tags.className], color: base0A, fontStyle: 'italic' },
{ tag: [highlight.tags.namespace], color: base06, fontStyle: 'italic' },
// Operators and punctuation
{ tag: [highlight.tags.operator, highlight.tags.operatorKeyword], color: base0E },
{ tag: [highlight.tags.bracket], color: base0E },
{ tag: [highlight.tags.brace], color: base0E },
{ tag: [highlight.tags.punctuation], color: base0E },
// Functions and parameters
{ tag: [highlight.tags.function(highlight.tags.variableName), highlight.tags.labelName], color: base07 },
{ tag: [highlight.tags.definition(highlight.tags.variableName)], color: base07 },
// Constants and literals
{ tag: highlight.tags.number, color: base09 },
{ tag: highlight.tags.changed, color: base09 },
{ tag: highlight.tags.annotation, color: invalid, fontStyle: 'italic' },
{ tag: highlight.tags.modifier, color: base09, fontStyle: 'italic' },
{ tag: highlight.tags.self, color: base09 },
{ tag: [highlight.tags.color, highlight.tags.constant(highlight.tags.name), highlight.tags.standard(highlight.tags.name)], color: base09, fontWeight: 'bold' },
{ tag: [highlight.tags.atom, highlight.tags.bool, highlight.tags.special(highlight.tags.variableName)], color: base09 },
// Strings and regex
{ tag: [highlight.tags.processingInstruction, highlight.tags.inserted], color: base08 },
{ tag: [highlight.tags.special(highlight.tags.string), highlight.tags.regexp], color: base08 },
{ tag: highlight.tags.string, color: base08 },
// Punctuation and structure
{ tag: highlight.tags.definition(highlight.tags.typeName), color: base0A, fontWeight: 'bold' },
// Comments and documentation
{ tag: highlight.tags.meta, color: base03 },
{ tag: highlight.tags.comment, fontStyle: 'italic', color: base03 },
{ tag: highlight.tags.docComment, fontStyle: 'italic', color: base03 },
// HTML/XML elements
{ tag: [highlight.tags.tagName], color: base0D },
{ tag: [highlight.tags.attributeName], color: base0C },
// Markdown and text formatting
{ tag: [highlight.tags.heading], fontWeight: 'bold', color: base0A },
{ tag: [highlight.tags.strong], fontWeight: 'bold', color: base05 },
{ tag: [highlight.tags.emphasis], fontStyle: 'italic', color: base06 },
// Links and URLs
{ tag: [highlight.tags.link], color: base0A, fontWeight: '500', textDecoration: 'underline', textUnderlinePosition: 'under' },
{
tag: [highlight.tags.url],
color: base0C,
textDecoration: 'underline',
textUnderlineOffset: '2px',
},
// Special states
{ tag: [highlight.tags.invalid], color: base01, textDecoration: 'underline wavy', borderBottom: `1px wavy ${invalid}` },
{ tag: [highlight.tags.strikethrough], color: invalid, textDecoration: 'line-through' },
// Enhanced syntax highlighting
{ tag: highlight.tags.constant(highlight.tags.name), color: base09 },
{ tag: highlight.tags.deleted, color: invalid },
{ tag: highlight.tags.squareBracket, color: base0E },
{ tag: highlight.tags.angleBracket, color: base0E },
// Additional specific styles
{ tag: highlight.tags.heading1, fontWeight: 'bold', color: base0A },
{ tag: highlight.tags.special(highlight.tags.heading1), fontWeight: 'bold', color: base0A },
{ tag: [highlight.tags.heading2, highlight.tags.heading3, highlight.tags.heading4], fontWeight: 'bold', color: base0A },
{ tag: [highlight.tags.heading5, highlight.tags.heading6], color: base0A },
{ tag: highlight.tags.monospace, color: base01 },
{ tag: [highlight.tags.contentSeparator], color: base0C },
{ tag: highlight.tags.quote, color: base03 },
]);
/**
* Combined Forest theme extension
*/
const forest = [
forestTheme,
language.syntaxHighlighting(forestHighlightStyle),
];
/**
* Forest merge revert styles configuration
*/
const forestMergeStyles = {
backgroundColor: darkBackground,
borderColor: '#3e5059',
buttonColor: base01,
buttonHoverColor: '#2d3d47',
};
exports.applyMergeRevertStyles = applyMergeRevertStyles;
exports.forest = forest;
exports.forestMergeStyles = forestMergeStyles;