Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
66.67% |
2 / 3 |
CRAP | |
17.07% |
7 / 41 |
FileType | |
0.00% |
0 / 1 |
|
66.67% |
2 / 3 |
13.12 | |
17.07% |
7 / 41 |
from | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
getSyntaxFromFilename | |
0.00% |
0 / 1 |
4.87 | |
10.53% |
4 / 38 |
|||
__construct | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
1 | <?php declare(strict_types=1); |
2 | |
3 | namespace Aviat\Kilo; |
4 | |
5 | class FileType { |
6 | |
7 | public static function from(?string $filename): self |
8 | { |
9 | $syntax = self::getSyntaxFromFilename((string)$filename); |
10 | |
11 | return new self($syntax->filetype, $syntax); |
12 | } |
13 | |
14 | private static function getSyntaxFromFilename(string $filename): Syntax |
15 | { |
16 | $ext = strstr(basename($filename), '.'); |
17 | $ext = ($ext !== FALSE) ? $ext : ''; |
18 | return match ($ext) { |
19 | '.sh', '.bash' => Syntax::new( |
20 | 'Shell', |
21 | slcs: '#', |
22 | mcs: '', |
23 | mce: '', |
24 | ), |
25 | '.php', 'kilo' => Syntax::new( |
26 | 'PHP', |
27 | ), |
28 | '.c', '.h', '.cpp', '.cxx', '.cc', '.hpp' => Syntax::new( |
29 | 'C', |
30 | [ |
31 | 'auto', 'break', 'case', 'const', 'continue', 'default', 'do', 'typedef', 'switch', 'return', |
32 | 'static', 'while', 'break', 'struct', 'extern', 'union', 'class', 'else', 'enum', 'for', 'case', |
33 | 'if', 'inline', 'register', 'restrict', 'return', 'sizeof', 'switch', 'typedef', 'union', 'volatile' |
34 | ], |
35 | [ |
36 | '#include', 'unsigned', '#define', '#ifndef', 'double', 'signed', '#endif', |
37 | '#ifdef', 'float', '#error', '#undef', '#elif', 'long', 'char', 'int', 'void', '#if', |
38 | 'uint32_t', 'wchar_t', 'int32_t', 'int64_t', 'uint64_t', 'int16_t', 'uint16_t', |
39 | 'uint8_t', 'int8_t', |
40 | ], |
41 | [ |
42 | '<=>', '<<=', '>>=', |
43 | '++', '--', '==', '!=', '>=', '<=', '&&', '||', '<<', '>>', |
44 | '+=', '-=', '*=', '/=', '%=', '&=', '|=', '^=', '->', '::', |
45 | ], |
46 | hasCharType: true, |
47 | highlightCharacters: true, |
48 | ), |
49 | '.css', '.less', '.sass', '.scss' => Syntax::new( |
50 | 'CSS', |
51 | slcs: '', |
52 | ), |
53 | '.go' => Syntax::new( |
54 | 'Go', |
55 | [ |
56 | 'break', 'case', 'chan', 'const', 'continue', 'default', 'defer', 'else', |
57 | 'fallthrough', 'for', 'func', 'go', 'goto', 'if', 'import', 'interface', |
58 | 'map', 'package', 'range', 'return', 'select', 'struct', 'switch', 'type', 'var', |
59 | ], |
60 | [ |
61 | 'uint8', 'uint16', 'uint32', 'uint64', 'int8', 'int16', 'int32', 'int64', 'float32', 'float64', |
62 | 'uint', 'int', 'uintptr', 'complex64', 'complex128', 'byte', 'rune', '[]', |
63 | ], |
64 | [ |
65 | '&^=', '...', '>>=', '<<=', '--', '%=', '>>', ':=', '++', '/=', '<<', '>=', |
66 | '<-', '^=', '*=', '<=', '||', '|=', '-=', '!=', '==', '&&', '&=', '+=', |
67 | ], |
68 | hasCharType: true, |
69 | highlightCharacters: true, |
70 | ), |
71 | '.js', '.jsx', '.ts', '.tsx', '.jsm', '.mjs', '.es' => Syntax::new( |
72 | 'JavaScript', |
73 | [ |
74 | 'instanceof', 'continue', 'debugger', 'function', 'default', 'extends', |
75 | 'finally', 'delete', 'export', 'import', 'return', 'switch', 'typeof', |
76 | 'break', 'catch', 'class', 'const', 'super', 'throw', 'while', 'yield', |
77 | 'case', 'else', 'this', 'void', 'with', 'from', 'for', 'new', 'try', |
78 | 'var', 'do', 'if', 'in', 'as', |
79 | ], |
80 | [ |
81 | '=>', 'Number', 'String', 'Object', 'Math', 'JSON', 'Boolean', |
82 | ], |
83 | ), |
84 | '.rs' => Syntax::new( |
85 | 'Rust', |
86 | [ |
87 | 'continue', 'return', 'static', 'struct', 'unsafe', 'break', 'const', 'crate', |
88 | 'extern', 'match', 'super', 'trait', 'where', 'else', 'enum', 'false', 'impl', |
89 | 'loop', 'move', 'self', 'type', 'while', 'for', 'let', 'mod', 'pub', 'ref', 'true', |
90 | 'use', 'mut', 'as', 'fn', 'if', 'in', |
91 | ], |
92 | [ |
93 | 'DoubleEndedIterator', 'ExactSizeIterator', 'IntoIterator', 'PartialOrd', 'PartialEq', |
94 | 'Iterator', 'ToString', 'Default', 'ToOwned', 'Extend', 'FnOnce', 'Option', 'String', |
95 | 'AsMut', 'AsRef', 'Clone', 'Debug', 'FnMut', 'Sized', 'Unpin', 'array', 'isize', |
96 | 'usize', '&str', 'Copy', 'Drop', 'From', 'Into', 'None', 'Self', 'Send', 'Some', |
97 | 'Sync', 'bool', 'char', 'i128', 'u128', 'Box', 'Err', 'Ord', 'Vec', 'dyn', 'f32', |
98 | 'f64', 'i16', 'i32', 'i64', 'str', 'u16', 'u32', 'u64', 'Eq', 'Fn', 'Ok', 'i8', 'u8', |
99 | ], |
100 | hasCharType: true, |
101 | highlightCharacters: true, |
102 | ), |
103 | default => Syntax::default(), |
104 | }; |
105 | } |
106 | |
107 | private function __construct(public string $name, public Syntax $syntax) {} |
108 | } |