php-kilo/src/FileType.php

115 lines
4.0 KiB
PHP
Raw Normal View History

2021-03-16 18:37:53 -04:00
<?php declare(strict_types=1);
namespace Aviat\Kilo;
class FileType {
public static function from(?string $filename): self
{
$syntax = self::getSyntaxFromFilename((string)$filename);
return new self($syntax->filetype, $syntax);
}
private static function getSyntaxFromFilename(string $filename): Syntax
{
2021-03-17 15:38:52 -04:00
$ext = strstr(basename($filename), '.');
$ext = ($ext !== FALSE) ? $ext : '';
2021-03-16 18:37:53 -04:00
return match ($ext) {
2021-04-14 12:25:41 -04:00
'.sh', '.bash' => Syntax::new(
'Shell',
2021-04-14 17:19:01 -04:00
[
'case', 'do', 'done', 'elif', 'else', 'esac', 'fi', 'for', 'function', 'if', 'in',
'select', 'then', 'time', 'until', 'while', 'declare',
],
['set'],
2021-04-14 12:25:41 -04:00
slcs: '#',
mcs: '',
mce: '',
2021-04-14 17:19:01 -04:00
highlightNumbers: false,
2021-04-14 12:25:41 -04:00
),
2021-03-16 18:37:53 -04:00
'.php', 'kilo' => Syntax::new(
'PHP',
),
'.c', '.h', '.cpp', '.cxx', '.cc', '.hpp' => Syntax::new(
'C',
[
'auto', 'break', 'case', 'const', 'continue', 'default', 'do', 'typedef', 'switch', 'return',
'static', 'while', 'break', 'struct', 'extern', 'union', 'class', 'else', 'enum', 'for', 'case',
'if', 'inline', 'register', 'restrict', 'return', 'sizeof', 'switch', 'typedef', 'union', 'volatile'
2021-03-16 18:37:53 -04:00
],
[
'#include', 'unsigned', '#define', '#ifndef', 'double', 'signed', '#endif',
'#ifdef', 'float', '#error', '#undef', '#elif', 'long', 'char', 'int', 'void', '#if',
'uint32_t', 'wchar_t', 'int32_t', 'int64_t', 'uint64_t', 'int16_t', 'uint16_t',
'uint8_t', 'int8_t',
],
[
'<=>', '<<=', '>>=',
'++', '--', '==', '!=', '>=', '<=', '&&', '||', '<<', '>>',
'+=', '-=', '*=', '/=', '%=', '&=', '|=', '^=', '->', '::',
2021-03-16 18:37:53 -04:00
],
hasCharType: true,
highlightCharacters: true,
2021-03-16 18:37:53 -04:00
),
'.css', '.less', '.sass', '.scss' => Syntax::new(
'CSS',
slcs: '',
2021-04-14 17:19:01 -04:00
hasCommonOperators: false,
2021-03-16 18:37:53 -04:00
),
'.go' => Syntax::new(
'Go',
[
'break', 'case', 'chan', 'const', 'continue', 'default', 'defer', 'else',
'fallthrough', 'for', 'func', 'go', 'goto', 'if', 'import', 'interface',
'map', 'package', 'range', 'return', 'select', 'struct', 'switch', 'type', 'var',
],
[
'uint8', 'uint16', 'uint32', 'uint64', 'int8', 'int16', 'int32', 'int64', 'float32', 'float64',
'uint', 'int', 'uintptr', 'complex64', 'complex128', 'byte', 'rune', '[]',
],
[
'&^=', '...', '>>=', '<<=', '--', '%=', '>>', ':=', '++', '/=', '<<', '>=',
'<-', '^=', '*=', '<=', '||', '|=', '-=', '!=', '==', '&&', '&=', '+=',
],
hasCharType: true,
highlightCharacters: true,
),
2021-03-16 18:37:53 -04:00
'.js', '.jsx', '.ts', '.tsx', '.jsm', '.mjs', '.es' => Syntax::new(
'JavaScript',
[
'instanceof', 'continue', 'debugger', 'function', 'default', 'extends',
'finally', 'delete', 'export', 'import', 'return', 'switch', 'typeof',
'break', 'catch', 'class', 'const', 'super', 'throw', 'while', 'yield',
'case', 'else', 'this', 'void', 'with', 'from', 'for', 'new', 'try',
'var', 'do', 'if', 'in', 'as',
],
[
'=>', 'Number', 'String', 'Object', 'Math', 'JSON', 'Boolean',
],
),
'.rs' => Syntax::new(
'Rust',
[
'continue', 'return', 'static', 'struct', 'unsafe', 'break', 'const', 'crate',
'extern', 'match', 'super', 'trait', 'where', 'else', 'enum', 'false', 'impl',
'loop', 'move', 'self', 'type', 'while', 'for', 'let', 'mod', 'pub', 'ref', 'true',
'use', 'mut', 'as', 'fn', 'if', 'in',
],
[
'DoubleEndedIterator', 'ExactSizeIterator', 'IntoIterator', 'PartialOrd', 'PartialEq',
'Iterator', 'ToString', 'Default', 'ToOwned', 'Extend', 'FnOnce', 'Option', 'String',
'AsMut', 'AsRef', 'Clone', 'Debug', 'FnMut', 'Sized', 'Unpin', 'array', 'isize',
'usize', '&str', 'Copy', 'Drop', 'From', 'Into', 'None', 'Self', 'Send', 'Some',
'Sync', 'bool', 'char', 'i128', 'u128', 'Box', 'Err', 'Ord', 'Vec', 'dyn', 'f32',
'f64', 'i16', 'i32', 'i64', 'str', 'u16', 'u32', 'u64', 'Eq', 'Fn', 'Ok', 'i8', 'u8',
],
hasCharType: true,
highlightCharacters: true,
2021-03-16 18:37:53 -04:00
),
default => Syntax::default(),
};
}
private function __construct(public string $name, public Syntax $syntax) {}
}