UNPKG

greybel-languageserver-core

Version:
133 lines 5.17 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.activate = void 0; const color_convert_1 = __importDefault(require("color-convert")); const miniscript_core_1 = require("miniscript-core"); var ColorType; (function (ColorType) { ColorType["Black"] = "black"; ColorType["Blue"] = "blue"; ColorType["Green"] = "green"; ColorType["Orange"] = "orange"; ColorType["Purple"] = "purple"; ColorType["Red"] = "red"; ColorType["White"] = "white"; ColorType["Yellow"] = "yellow"; })(ColorType || (ColorType = {})); const ColorMap = { black: '#000000', blue: '#0000FF', green: '#00FF00', orange: '#FF8800', purple: '#CC8899', red: '#FF0000', white: '#FFFFFF', yellow: '#FFFF00' }; const createColorRegExp = () => new RegExp(`(?:mark|color)=(${Object.keys(ColorMap).join('|')}|(?:#[0-9a-f]{6}|#[0-9a-f]{3}))`, 'ig'); const hasOwnProperty = Object.prototype.hasOwnProperty; function activate(context) { context.connection.onColorPresentation((params) => { return [ { label: `#${color_convert_1.default.rgb.hex(params.color.red * 255, params.color.green * 255, params.color.blue * 255)}` } ]; }); context.connection.onDocumentColor(async (params) => { const textDocument = await context.fs.getTextDocument(params.textDocument.uri); if (textDocument == null) { return; } const parseResult = await context.documentManager.getLatest(textDocument); const chunk = parseResult.parsedPayload; const allAvailableStrings = chunk.literals.filter((literal) => literal.type === miniscript_core_1.ASTType.StringLiteral); const result = []; const getRange = ({ match, markup, value, astPosition, lineIndex }) => { const colorStartIndex = match.index + markup.indexOf('=') + 1; const colorEndIndex = colorStartIndex + value.length; const line = astPosition.line - 1 + lineIndex; let start = colorStartIndex; let end = colorEndIndex; if (lineIndex === 0) { start += astPosition.character; end += astPosition.character; } const colorStart = { line, character: start }; const colorEnd = { line, character: end }; return { start: colorStart, end: colorEnd }; }; for (let index = 0; index < allAvailableStrings.length; index++) { const strLiteral = allAvailableStrings[index]; if (!strLiteral.start) continue; const start = strLiteral.start; const lines = strLiteral.value.toString().split('\n'); for (let lineIndex = 0; lineIndex < lines.length; lineIndex++) { const line = lines[lineIndex]; const regexp = createColorRegExp(); let match; while ((match = regexp.exec(line))) { const [markup, value] = match; const range = getRange({ match, markup, value, astPosition: start, lineIndex }); if (value.startsWith('#')) { const [red, green, blue] = color_convert_1.default.hex.rgb(value.slice(1)); result.push({ range, color: { red: red / 255, green: green / 255, blue: blue / 255, alpha: 1 } }); } else if (hasOwnProperty.call(ColorMap, value)) { const [red, green, blue] = color_convert_1.default.hex.rgb(ColorMap[value].slice(1)); result.push({ range, color: { red: red / 255, green: green / 255, blue: blue / 255, alpha: 1 } }); } else { result.push({ range, color: { red: 0, green: 0, blue: 0, alpha: 1 } }); } } } } return result; }); } exports.activate = activate; //# sourceMappingURL=color.js.map