Skip to content

Commit

Permalink
feat: add c/cpp support
Browse files Browse the repository at this point in the history
  • Loading branch information
blurfx committed Nov 25, 2023
1 parent 6d0a7a0 commit fdcb6d8
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 1 deletion.
41 changes: 41 additions & 0 deletions packages/core/src/rules/cpp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { ParseRule } from '../types';

const cppRules: ParseRule[] = [
{
kind: 'comment',
pattern: /(\/\/.*\n?)|\/\*[^]*?\*\//g,
},
{
kind: 'keyword',
pattern:
/\b(auto|break|case|char|const|continue|default|do|double|else|enum|extern|float|for|goto|if|int|long|register|return|short|signed|sizeof|static|struct|switch|typedef|union|unsigned|void|volatile|while|bool|true|false|catch|class|const_cast|delete|dynamic_cast|explicit|export|friend|inline|mutable|namespace|new|operator|private|protected|public|reinterpret_cast|static_cast|template|this|throw|try|typeid|typename|using|virtual|nullptr|null|string|and|and_eq|bitand|bitor|compl|not|not_eq|or|or_eq|xor|xor_eq)\b/g,
},
{
kind: 'string',
pattern: /R"([^()\n]*)"|L?"([^"\\]*(\\.[^"\\]*)*)"/g,
},
{
kind: 'number',
pattern:
/\b(0b[01_]+|0[0-7_]*|0x[\da-fA-F_]+|\d[\d_]*(.\d[\d_]*)?(e[\+\-]?\d[\d_]*)?)(u|ul|ull|l|ll|f|F|L)?\b/g,
},
{
kind: 'class',
pattern: /#.*(?=\n|$)/g,
},
{
kind: 'operator',
// pattern: /[+\-*/%&|^!~=<>?:]+|::|\(\)?|\[\]?|\{\}?/g,
pattern: /[+\-*/%&~|^!=<>?:]+/g,
},
{
kind: 'class',
pattern: /\b[A-Z_][\w_]*\b/g,
},
{
kind: 'symbol',
pattern: /[a-zA-Z_]\w*/g,
},
];

export default cppRules;
2 changes: 2 additions & 0 deletions packages/core/src/rules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import { ParseRule } from '../types';
import javascriptRules from './javascript';
import typescriptRules from './typescript';
import golangRules from './golang';
import cppRules from './cpp';

const Rules: Record<string, ParseRule[]> = {
javascript: javascriptRules,
typescript: typescriptRules,
golang: golangRules,
cpp: cppRules,
};

export const getParseRule = (lang: string) => {
Expand Down
1 change: 1 addition & 0 deletions packages/preview/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"type": "module",
"scripts": {
"dev": "vite",
"build:deps": "pnpm -w -F core,highlighter run build",
"build": "tsc && vite build",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview"
Expand Down
161 changes: 161 additions & 0 deletions packages/preview/public/examples/cpp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
// slice of https://github.com/git/git/blob/564d0252ca632e0264ed670534a51d18a689ef5d/branch.c

#include "git-compat-util.h"
#include "advice.h"
#include "config.h"
#include "branch.h"
#include "environment.h"
#include "gettext.h"
#include "hex.h"
#include "object-name.h"
#include "path.h"
#include "refs.h"
#include "refspec.h"
#include "remote.h"
#include "repository.h"
#include "sequencer.h"
#include "commit.h"
#include "worktree.h"
#include "submodule-config.h"
#include "run-command.h"
#include "strmap.h"

struct tracking {
struct refspec_item spec;
struct string_list *srcs;
const char *remote;
int matches;
};

struct find_tracked_branch_cb {
struct tracking *tracking;
struct string_list ambiguous_remotes;
};

/**
* Install upstream tracking configuration for a branch; specifically, add
* `branch.<name>.remote` and `branch.<name>.merge` entries.
*
* `flag` contains integer flags for options; currently only
* BRANCH_CONFIG_VERBOSE is checked.
*
* `local` is the name of the branch whose configuration we're installing.
*
* `origin` is the name of the remote owning the upstream branches. NULL means
* the upstream branches are local to this repo.
*
* `remotes` is a list of refs that are upstream of local
*/
static int install_branch_config_multiple_remotes(int flag, const char *local,
const char *origin, struct string_list *remotes)
{
const char *shortname = NULL;
struct strbuf key = STRBUF_INIT;
struct string_list_item *item;
int rebasing = should_setup_rebase(origin);

if (!remotes->nr)
BUG("must provide at least one remote for branch config");
if (rebasing && remotes->nr > 1)
die(_("cannot inherit upstream tracking configuration of "
"multiple refs when rebasing is requested"));

/*
* If the new branch is trying to track itself, something has gone
* wrong. Warn the user and don't proceed any further.
*/
if (!origin)
for_each_string_list_item(item, remotes)
if (skip_prefix(item->string, "refs/heads/", &shortname)
&& !strcmp(local, shortname)) {
warning(_("not setting branch '%s' as its own upstream"),
local);
return 0;
}

strbuf_addf(&key, "branch.%s.remote", local);
if (git_config_set_gently(key.buf, origin ? origin : ".") < 0)
goto out_err;

strbuf_reset(&key);
strbuf_addf(&key, "branch.%s.merge", local);
/*
* We want to overwrite any existing config with all the branches in
* "remotes". Override any existing config, then write our branches. If
* more than one is provided, use CONFIG_REGEX_NONE to preserve what
* we've written so far.
*/
if (git_config_set_gently(key.buf, NULL) < 0)
goto out_err;
for_each_string_list_item(item, remotes)
if (git_config_set_multivar_gently(key.buf, item->string, CONFIG_REGEX_NONE, 0) < 0)
goto out_err;

if (rebasing) {
strbuf_reset(&key);
strbuf_addf(&key, "branch.%s.rebase", local);
if (git_config_set_gently(key.buf, "true") < 0)
goto out_err;
}
strbuf_release(&key);

if (flag & BRANCH_CONFIG_VERBOSE) {
struct strbuf tmp_ref_name = STRBUF_INIT;
struct string_list friendly_ref_names = STRING_LIST_INIT_DUP;

for_each_string_list_item(item, remotes) {
shortname = item->string;
skip_prefix(shortname, "refs/heads/", &shortname);
if (origin) {
strbuf_addf(&tmp_ref_name, "%s/%s",
origin, shortname);
string_list_append_nodup(
&friendly_ref_names,
strbuf_detach(&tmp_ref_name, NULL));
} else {
string_list_append(
&friendly_ref_names, shortname);
}
}

if (remotes->nr == 1) {
/*
* Rebasing is only allowed in the case of a single
* upstream branch.
*/
printf_ln(rebasing ?
_("branch '%s' set up to track '%s' by rebasing.") :
_("branch '%s' set up to track '%s'."),
local, friendly_ref_names.items[0].string);
} else {
printf_ln(_("branch '%s' set up to track:"), local);
for_each_string_list_item(item, &friendly_ref_names)
printf_ln(" %s", item->string);
}

string_list_clear(&friendly_ref_names, 0);
}

return 0;

out_err:
strbuf_release(&key);
error(_("unable to write upstream branch configuration"));

advise(_("\nAfter fixing the error cause you may try to fix up\n"
"the remote tracking information by invoking:"));
if (remotes->nr == 1)
advise(" git branch --set-upstream-to=%s%s%s",
origin ? origin : "",
origin ? "/" : "",
remotes->items[0].string);
else {
advise(" git config --add branch.\"%s\".remote %s",
local, origin ? origin : ".");
for_each_string_list_item(item, remotes)
advise(" git config --add branch.\"%s\".merge %s",
local, item->string);
}

return -1;
}
2 changes: 2 additions & 0 deletions packages/preview/public/examples/golang.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// https://xo.dev/articles/how-search-engine-works

package main

import (
Expand Down
3 changes: 2 additions & 1 deletion packages/preview/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import darkTheme from '@calor/highlighter/themes/github-dark.css?inline';
const exampleCode: Record<string, string> = {
typescript: 'typescript.ts',
golang: 'golang.go',
cpp: 'cpp.cpp',
};

const fetchFile = async (path: string) => {
Expand All @@ -21,7 +22,7 @@ function App() {
useEffect(() => {
(async () => {
const text = await fetchFile(`./examples/${exampleCode[language]}`);
const highlightedCode = highlight(text);
const highlightedCode = highlight(text, language);
setCode(highlightedCode);
})();
}, [language]);
Expand Down

0 comments on commit fdcb6d8

Please sign in to comment.