From 6d9d1113f2f60b92971508a3343d2e4b801cbd3b Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Wed, 17 Jul 2024 10:57:06 -0400 Subject: [PATCH] More Filetype information, tweak highlighting colors slightly --- src/common/filetype/filetype.ts | 104 +++++++++++++++++--------------- src/common/highlight.ts | 6 +- 2 files changed, 59 insertions(+), 51 deletions(-) diff --git a/src/common/filetype/filetype.ts b/src/common/filetype/filetype.ts index ed75100..4ed8c7a 100644 --- a/src/common/filetype/filetype.ts +++ b/src/common/filetype/filetype.ts @@ -9,6 +9,7 @@ export enum FileLang { TypeScript = 'TypeScript', JavaScript = 'JavaScript', CSS = 'CSS', + Shell = 'Shell', Plain = 'Plain Text', } @@ -20,12 +21,15 @@ export interface HighlightingOptions { interface IFileType { readonly name: FileLang; readonly singleLineComment: Option; + readonly multiLineCommentStart: Option; + readonly multiLineCommentEnd: Option; readonly keywords1: string[]; readonly keywords2: string[]; readonly hlOptions: HighlightingOptions; get flags(): HighlightingOptions; get primaryKeywords(): string[]; get secondaryKeywords(): string[]; + hasMultilineComments(): boolean; } /** @@ -34,6 +38,8 @@ interface IFileType { export abstract class AbstractFileType implements IFileType { public readonly name: FileLang = FileLang.Plain; public readonly singleLineComment = None; + public readonly multiLineCommentStart: Option = None; + public readonly multiLineCommentEnd: Option = None; public readonly keywords1: string[] = []; public readonly keywords2: string[] = []; public readonly hlOptions: HighlightingOptions = { @@ -52,6 +58,11 @@ export abstract class AbstractFileType implements IFileType { get secondaryKeywords(): string[] { return this.keywords2; } + + public hasMultilineComments(): boolean { + return this.multiLineCommentStart.isSome() && + this.multiLineCommentEnd.isSome(); + } } // ---------------------------------------------------------------------------- @@ -65,6 +76,8 @@ const defaultHighlightOptions: HighlightingOptions = { class JavaScriptFile extends AbstractFileType { public readonly name: FileLang = FileLang.JavaScript; public readonly singleLineComment = Some('//'); + public readonly multiLineCommentStart: Option = Some('/*'); + public readonly multiLineCommentEnd: Option = Some('*/'); public readonly keywords1 = [ 'break', 'case', @@ -115,6 +128,13 @@ class JavaScriptFile extends AbstractFileType { 'get', 'of', 'set', + '=>', + 'Number', + 'String', + 'Object', + 'Math', + 'JSON', + 'Boolean', ]; public readonly hlOptions: HighlightingOptions = { ...defaultHighlightOptions, @@ -123,56 +143,8 @@ class JavaScriptFile extends AbstractFileType { class TypeScriptFile extends JavaScriptFile { public readonly name: FileLang = FileLang.TypeScript; - public readonly keywords1 = [ - 'break', - 'case', - 'catch', - 'class', - 'const', - 'continue', - 'debugger', - 'default', - 'delete', - 'do', - 'else', - 'export', - 'extends', - 'false', - 'finally', - 'for', - 'function', - 'if', - 'import', - 'in', - 'instanceof', - 'new', - 'null', - 'return', - 'super', - 'switch', - 'this', - 'throw', - 'true', - 'try', - 'typeof', - 'var', - 'void', - 'while', - 'with', - 'let', - 'static', - 'yield', - 'await', - ]; public readonly keywords2 = [ - 'arguments', - 'as', - 'async', - 'eval', - 'from', - 'get', - 'of', - 'set', + ...super.secondaryKeywords, // Typescript-specific 'keyof', 'interface', @@ -189,9 +161,40 @@ class TypeScriptFile extends JavaScriptFile { ]; } +class ShellFile extends AbstractFileType { + public readonly name: FileLang = FileLang.Shell; + public readonly singleLineComment = Some('#'); + public readonly keywords1 = [ + 'case', + 'do', + 'done', + 'elif', + 'else', + 'esac', + 'fi', + 'for', + 'function', + 'if', + 'in', + 'select', + 'then', + 'time', + 'until', + 'while', + 'declare', + ]; + public readonly keywords2 = ['set']; + public readonly hlOptions: HighlightingOptions = { + ...defaultHighlightOptions, + numbers: false, + }; +} + class CSSFile extends AbstractFileType { public readonly name: FileLang = FileLang.CSS; public readonly singleLineComment = None; + public readonly multiLineCommentStart: Option = Some('/*'); + public readonly multiLineCommentEnd: Option = Some('*/'); public readonly hlOptions: HighlightingOptions = { ...defaultHighlightOptions, }; @@ -204,9 +207,12 @@ class CSSFile extends AbstractFileType { export class FileType extends AbstractFileType { static #fileTypeMap = new Map([ ['.css', CSSFile], + ['.json', JavaScriptFile], ['.js', JavaScriptFile], ['.jsx', JavaScriptFile], ['.mjs', JavaScriptFile], + ['.bash', ShellFile], + ['.sh', ShellFile], ['.ts', TypeScriptFile], ['.tsx', TypeScriptFile], ]); diff --git a/src/common/highlight.ts b/src/common/highlight.ts index d1ef0ce..3d3abe0 100644 --- a/src/common/highlight.ts +++ b/src/common/highlight.ts @@ -6,6 +6,7 @@ export enum HighlightType { Match, String, SingleLineComment, + MultiLineComment, Keyword1, Keyword2, } @@ -19,10 +20,11 @@ export function highlightToColor(type: HighlightType): string { return Ansi.color256(21); case HighlightType.String: - return Ansi.color256(201); + return Ansi.color256(45); case HighlightType.SingleLineComment: - return Ansi.color256(45); + case HighlightType.MultiLineComment: + return Ansi.color256(201); case HighlightType.Keyword1: return Ansi.color256(226);