diff --git a/CHANGELOG.md b/CHANGELOG.md index 6ab475f..f2348cb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,12 @@ # Change Log -All notable changes to the "vscode-lotro-api" extension will be documented in this file. - -Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file. +## [1.0.8] - 2022-03-30 +### Added +- Improvement: Adding support for deprecated lua function from 5.1 +- Improvement: Adding Attributes stance enum classes +- Improvement: Adding undocumented IsA function to base Turbine.Object class +- Improvement: In editor color display and color picker for Turbine.UI.Color classes + ![](https://github.com/lunarwtr/vscode-lotro-api/raw/main/img/color2.gif) ## [1.0.7] - 2022-03-27 ### Added diff --git a/img/color2.gif b/img/color2.gif new file mode 100755 index 0000000..4fecb7a Binary files /dev/null and b/img/color2.gif differ diff --git a/package.json b/package.json index 671fe22..e2ee946 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "lotro-api", "displayName": "LotRO API", "description": "Lord of the Rings Online API", - "version": "1.0.7", + "version": "1.0.8", "publisher": "lunarwtr", "repository": { "type": "git", diff --git a/src/LuaColorShow.ts b/src/LuaColorShow.ts new file mode 100644 index 0000000..5e4e7c1 --- /dev/null +++ b/src/LuaColorShow.ts @@ -0,0 +1,61 @@ +import { + DocumentColorProvider, + TextDocument, + CancellationToken, + ProviderResult, + Color, + ColorPresentation, + ColorInformation, + Range +} from "vscode"; +import { Colors as TurbineColors, ColorLookup as TurbineColorLookup, roundRgb } from "./colors"; + +class LuaColorShow implements DocumentColorProvider { + provideDocumentColors(document: TextDocument, token: CancellationToken): ProviderResult { + if (document.languageId !== 'lua') { + return; + } + let colors: ColorInformation[] = []; + const regEx = /Turbine.UI.Color(\.(\w+)|\((\s*\d*(\.\d+)?\s*(,\s*\d*(\.\d+)?\s*){2,3})\))/g; + const text = document.getText(); + let match; + while ((match = regEx.exec(text))) { + let color; + if (match[1].startsWith('.')) { + const c = TurbineColors[match[2]]; + if (!c) { + // not a legit Turbine Color + continue; + } + color = new Color(c.R, c.G, c.B, c.A); + } else { + const c = match[3].trim().split(/\s*,\s*/).map(n => parseFloat(n)); + if (c.length === 3) { + c.unshift(1); + } + if (c.filter(n => n > 1).length > 0) { + // invalid color value + continue; + } + color = new Color(c[1], c[2], c[3], c[0]); + } + + const startPos = document.positionAt(match.index); + const endPos = document.positionAt(match.index + match[0].length); + colors.push(new ColorInformation(new Range(startPos, endPos), color)); + } + return colors; + } + provideColorPresentations(color: Color, context: { document: TextDocument; range: Range; }, token: CancellationToken): ProviderResult { + const pieces = [color.alpha, color.red, color.green, color.blue].map(n => roundRgb(n)); + const knownColor = TurbineColorLookup[pieces.join(':')]; + if (knownColor) { + return [new ColorPresentation(`Turbine.UI.Color.${knownColor}`)]; + } + if (color.alpha === 1) { + pieces.shift(); + } + return [new ColorPresentation(`Turbine.UI.Color(${pieces.join(',')})`)]; + } +} +export default LuaColorShow; \ No newline at end of file diff --git a/src/colors.ts b/src/colors.ts index 06a5d5e..b7e8451 100644 --- a/src/colors.ts +++ b/src/colors.ts @@ -854,4 +854,16 @@ export const Colors: {[id: string] : IColor} = { "G": 1, "R": 1 } -}; \ No newline at end of file +}; + +export function roundRgb(rgb: number) { + return Math.round(rgb * 100) / 100; +} +export function colorToKey(color: IColor): string { + return `${roundRgb(color.A)}:${roundRgb(color.R)}:${roundRgb(color.G)}:${roundRgb(color.B)}`; +} +export const ColorLookup = Object.entries(Colors).reduce((p : { [key: string] : string }, c) => { + const key = colorToKey(c[1]); + p[key] = c[0]; + return p; +}, {}); diff --git a/src/extension.ts b/src/extension.ts index 7696f43..9cde424 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,6 +1,7 @@ import * as vscode from 'vscode'; import * as path from 'path'; import { Colors } from './colors'; +import LuaColorShow from './LuaColorShow'; interface XMLSchemaAssocation { pattern: string, @@ -61,90 +62,12 @@ export function activate(context: vscode.ExtensionContext) { filesConfig.update("associations", associations); } - console.log('decorator sample is activated'); - - let timeout: NodeJS.Timer | undefined = undefined; - - // create a decorator type that we use to decorate small numbers - const colorDecorationType = vscode.window.createTextEditorDecorationType({}); - - let activeEditor = vscode.window.activeTextEditor; - function updateDecorations() { - if (!activeEditor) { - return; - } - if (activeEditor.document.languageId !== 'lua') { - return; - } - const regEx = /Turbine.UI.Color(\.(\w+)|\((\s*\d*(\.\d+)?\s*(,\s*\d*(\.\d+)?\s*){2,3})\))/g; - const text = activeEditor.document.getText(); - const colors: vscode.DecorationOptions[] = []; - let match; - while ((match = regEx.exec(text))) { - let style = ''; - if (match[1].startsWith('.')) { - const c = Colors[match[2]]; - style = `rgba(${Math.ceil(255*c.R)},${Math.ceil(255*c.G)},${Math.ceil(255*c.B)},${c.A})`; - } else { - const c = match[3].trim().split(/\s*,\s*/).map(n => parseFloat(n)); - if (c.length === 3) { - c.unshift(1); - } - if (c.filter(n => n > 1).length > 0) { - // invalid color value - continue; - } - style = `rgba(${Math.ceil(255*c[1])},${Math.ceil(255*c[2])},${Math.ceil(255*c[3])},${c[0]})`; - } - - const startPos = activeEditor.document.positionAt(match.index); - const endPos = activeEditor.document.positionAt(match.index + match[0].length); - const decoration: vscode.DecorationOptions = { - range: new vscode.Range(startPos, endPos), - renderOptions: { - after: { - color: style, - contentText: '⬤', - margin: '0 0 0 2px' - } - } - }; - colors.push(decoration); - } - activeEditor.setDecorations(colorDecorationType, colors); - - } - - function triggerUpdateDecorations(throttle: boolean = false) { - if (timeout) { - clearTimeout(timeout); - timeout = undefined; - } - if (throttle) { - timeout = setTimeout(updateDecorations, 500); - } else { - updateDecorations(); - } - } - - if (activeEditor) { - triggerUpdateDecorations(); - } - vscode.window.onDidChangeTextEditorSelection((e) => { - console.log(e); - }); - vscode.window.onDidChangeActiveTextEditor(editor => { - activeEditor = editor; - if (editor) { - triggerUpdateDecorations(); - } - }, null, context.subscriptions); - - vscode.workspace.onDidChangeTextDocument(event => { - if (activeEditor && event.document === activeEditor.document) { - triggerUpdateDecorations(true); - } - }, null, context.subscriptions); + // Register our color provider for Turbine Colors + let luaColorShowDisposable = vscode.languages.registerColorProvider( + { pattern: "**/*.lua" }, + new LuaColorShow() + ); + context.subscriptions.push(luaColorShowDisposable); }