Skip to content

Commit

Permalink
Merge pull request #68 from teknologi-umum/language/dart
Browse files Browse the repository at this point in the history
feat: dart language support
  • Loading branch information
Reinaldy Rafli authored Dec 4, 2021
2 parents cbce1bd + a69219b commit d5c7703
Show file tree
Hide file tree
Showing 13 changed files with 894 additions and 160 deletions.
24 changes: 24 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Clojure } from './languages/clojure';
import { CPP } from './languages/cpp';
import { CS } from './languages/cs';
import { CSS } from './languages/css';
import { Dart } from './languages/dart';
import { Dockerfile } from './languages/dockerfile';
import { Elixir } from './languages/elixir';
import { Go } from './languages/go';
Expand All @@ -24,13 +25,15 @@ import { SQL } from './languages/sql';
import { YAML } from './languages/yaml';
import { nearTop, getPoints } from './points';
import { convert } from './shiki';
import { shebangMap } from './shebang';

const languages: Record<string, LanguagePattern[]> = {
C,
Clojure,
'C++': CPP,
'C#': CS,
CSS,
Dart,
Dockerfile,
Elixir,
Go,
Expand Down Expand Up @@ -81,6 +84,27 @@ function flourite(
});
}

// Shebang check
if (linesOfCode[0].startsWith('#!')) {
if (linesOfCode[0].startsWith('#!/usr/bin/env')) {
let language = linesOfCode[0].split(' ').slice(1).join(' ');
language = shebangMap[language] || language.charAt(0).toUpperCase() + language.slice(1);
return {
language: options.shiki ? convert(language) : language,
statistics: {},
linesOfCode: linesOfCode.length,
};
}

if (linesOfCode[0].startsWith('#!/bin/bash')) {
return {
language: options.shiki ? 'bash' : 'Bash',
statistics: {},
linesOfCode: linesOfCode.length,
};
}
}

const pairs = Object.keys(languages).map((key) => ({ language: key, checkers: languages[key] }));

const results: LanguagePoints[] = [];
Expand Down
2 changes: 2 additions & 0 deletions src/languages/c.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,6 @@ export const C: LanguagePattern[] = [
},
// Avoiding Lua confusion
{ pattern: /local\s(function|\w+)?/, type: 'not' },
// Avoiding Dart confusion
{ pattern: /^(void\s)?main\(\)\s(async\s)?{/, type: 'not' },
];
2 changes: 2 additions & 0 deletions src/languages/cpp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,6 @@ export const CPP: LanguagePattern[] = [
type: 'not',
},
{ pattern: /(const)?(\s+)?val(\s+)(.*)(:(\s)(.*)(\?)?)?(\s+)=(\s+)/, type: 'not' },
// Avoiding Dart confusion
{ pattern: /^(void\s)?main\(\)\s(async\s)?{/, type: 'not' },
];
2 changes: 2 additions & 0 deletions src/languages/cs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,6 @@ export const CS: LanguagePattern[] = [
{ pattern: /System\.(in|out)\.\w+/, type: 'not' },
// Avoiding Ruby confusion
{ pattern: /\bmodule\s\S/, type: 'not' },
// Avoiding Dart confusion
{ pattern: /^\s*import\s("|')dart:\w+("|')/, type: 'not' },
];
44 changes: 44 additions & 0 deletions src/languages/dart.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import type { LanguagePattern } from '../types';

export const Dart: LanguagePattern[] = [
// Variable declaration
{
pattern:
/^\s*(const|final|var|dynamic|late)?\s*(int|double|String|bool|List<[A-Za-z [\](),]+>|HashMap<[A-Za-z [\](),]+>|Iterator<[A-Za-z [\](),]+>|Set<[A-Za-z [\](),]+>)?(\?)?\s\w+(\s=\s.+)?(;|,)$/,
type: 'keyword.variable',
},
{ pattern: /\bstdout.write\(.+\);/, type: 'keyword.print' },
{ pattern: /\bprint\(.+\);/, type: 'keyword.print' },
{ pattern: /^\s*import\s("|')dart:\w+("|')/, type: 'meta.import', nearTop: true },
{ pattern: /^\s*import\s("|')package:\w+("|')/, type: 'meta.import', nearTop: true },
{ pattern: /^\s*library\s\w+;/, type: 'meta.module', nearTop: true },
{ pattern: /^\s*(void\s)?main\(\)\s(async\s)?{/, type: 'keyword.function' },
// function with type definition
{
pattern:
/^\s*(List<[A-Za-z [\](),]+>|HashMap<[A-Za-z [\](),]+>|int|double|String|bool|void|Iterator<[A-Za-z [\](),]+>|Set<[A-Za-z [\](),]+>)\s\w+\(.+\)\s*\{$/,
type: 'keyword.function',
},
// arrow function
{
pattern:
/^\s*(int|double|String|bool|List<[A-Za-z [\](),]+>|HashMap<[A-Za-z [\](),]+>|Iterator<[A-Za-z [\](),]+>|Set<[A-Za-z [\](),]+>)\s\w+\(.+\)\s=>/,
type: 'keyword.function',
},
{ pattern: /\bnew\s(List|Map|Iterator|HashMap|Set)<\w+>\(\);$/, type: 'keyword.variable' },
{
pattern: /^(abstract\s)?class\s\w+\s(extends\s\w+\s)?(with\s\w+\s)?(implements\s\w+\s)?{(})?$/,
type: 'keyword.control',
},
{ pattern: /\bget\s\w+=>\w+/, type: 'keyword.control' },
{ pattern: /^\s*@override$/, type: 'keyword.control' },
{ pattern: /\bset\s\w+\(.+\)/, type: 'keyword.control' },
{ pattern: /^\s*Future<w+>\s\w+\(.+\)\sasync/, type: 'keyword.control' },
{ pattern: /^\s*await\sfor/, type: 'keyword.control' },
{ pattern: /^\s*typedef\s.+\s=/, type: 'keyword.control' },
// Avoiding confusion with C
{ pattern: /\blong\s/, type: 'not' },
{ pattern: /\s*function\b/, type: 'not' },
// Avoiding confusion with Java
{ pattern: /\bArrayList/, type: 'not' },
];
4 changes: 3 additions & 1 deletion src/languages/java.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const Java: LanguagePattern[] = [
// while loop
{ pattern: /while\s+\(.+\)/, type: 'keyword.control' },
// void keyword
{ pattern: /void/g, type: 'keyword.other' },
{ pattern: /void/, type: 'keyword.other' },
// const
{ pattern: /const\s*\w+/, type: 'not' },
// pointer
Expand All @@ -53,4 +53,6 @@ export const Java: LanguagePattern[] = [
{ pattern: /fun main\((.*)?\) {/, type: 'not' },
{ pattern: /(inline(\s+))?fun(\s+)([A-Za-z0-9_])(\s+)?\((.*)\)(\s+)({|=)/, type: 'not' },
{ pattern: /(const)?(\s+)?val(\s+)(.*)(:(\s)(.*)(\?)?)?(\s+)=(\s+)/, type: 'not' },
// Avoiding Dart confusion
{ pattern: /^(void\s)?main\(\)\s{/, type: 'not' },
];
2 changes: 2 additions & 0 deletions src/languages/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,6 @@ export const Javascript: LanguagePattern[] = [
{ pattern: /fun main\((.*)?\) {/, type: 'not' },
{ pattern: /(inline(\s+))?fun(\s+)([A-Za-z0-9_])(\s+)?\((.*)\)(\s+)({|=)/, type: 'not' },
{ pattern: /(const)?(\s+)?val(\s+)(.*)(:(\s)(.*)(\?)?)?(\s+)=(\s+)/, type: 'not' },
// Avoiding Dart confusion
{ pattern: /^(void\s)?main()\s{/, type: 'not' },
];
9 changes: 9 additions & 0 deletions src/shebang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const shebangMap: Record<string, string> = {
node: 'Javascript',
jsc: 'Javascript',
rhino: 'Javascript',
deno: 'Typescript',
python3: 'Python',
python2: 'Python',
php: 'PHP',
};
Loading

0 comments on commit d5c7703

Please sign in to comment.