From be1fd6c2b3163a7f270e716e1c161482b7e4ef91 Mon Sep 17 00:00:00 2001 From: Tan Le Date: Wed, 14 Jun 2023 01:03:59 +1000 Subject: [PATCH] Add Code owners lexer --- docs/Languages.md | 13 ++--- lib/rouge/demos/codeowners | 6 +++ lib/rouge/lexers/codeowners.rb | 22 +++++++++ spec/visual/samples/codeowners | 87 ++++++++++++++++++++++++++++++++++ 4 files changed, 122 insertions(+), 6 deletions(-) create mode 100644 lib/rouge/demos/codeowners create mode 100644 lib/rouge/lexers/codeowners.rb create mode 100644 spec/visual/samples/codeowners diff --git a/docs/Languages.md b/docs/Languages.md index 710e6218b8..890600f5b7 100644 --- a/docs/Languages.md +++ b/docs/Languages.md @@ -23,22 +23,23 @@ - BrightScript (`brightscript`) - 1C (BSL) (`bsl`) - C (`c`) -- Ceylon (`ceylon`) +- C# (`csharp`) +- C++ (`cpp`) - CFScript (`cfscript`) +- CMHG (`cmhg`) +- CMake (`cmake`) +- CODEOWNERS (`codeowners`) +- CSS (`css`) +- Ceylon (`ceylon`) - Cisco IOS (`cisco_ios`) - Clean (`clean`) - Clojure (`clojure`) -- CMake (`cmake`) -- CMHG (`cmhg`) - CoffeeScript (`coffeescript`) - Common Lisp (`common_lisp`) - Config File (`conf`) - Console (`console`) - Coq (`coq`) -- C++ (`cpp`) - Crystal (`crystal`) -- C# (`csharp`) -- CSS (`css`) - csvs (`csvs`) - CUDA (`cuda`) - Cypher (`cypher`) diff --git a/lib/rouge/demos/codeowners b/lib/rouge/demos/codeowners new file mode 100644 index 0000000000..e4d88dcf69 --- /dev/null +++ b/lib/rouge/demos/codeowners @@ -0,0 +1,6 @@ +# Specify a default Code Owner by using a wildcard: +* @default-codeowner + +/docs/ @all-docs + +[Development] @dev-team diff --git a/lib/rouge/lexers/codeowners.rb b/lib/rouge/lexers/codeowners.rb new file mode 100644 index 0000000000..c87cc49ffb --- /dev/null +++ b/lib/rouge/lexers/codeowners.rb @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- # +# frozen_string_literal: true + +module Rouge + module Lexers + class Codeowners < RegexLexer + title 'CODEOWNERS' + desc 'Code Owners syntax (https://docs.gitlab.com/ee/user/project/codeowners/reference.html)' + tag 'codeowners' + filenames 'CODEOWNERS' + + state :root do + rule %r/\^?\[.*\]/, Name::Namespace + rule %r/^\s*#.*$/, Comment::Single + rule %r/[~\*\/\\]/, Operator + rule %r/[\p{Word}\.\-_\s]+/, Name::Variable + rule %r/\S*@\S+/, Name::Function + rule %r/[ \t\r]+/, Text + end + end + end +end diff --git a/spec/visual/samples/codeowners b/spec/visual/samples/codeowners new file mode 100644 index 0000000000..da02f4dc8d --- /dev/null +++ b/spec/visual/samples/codeowners @@ -0,0 +1,87 @@ +# This is an example of a CODEOWNERS file. +# Lines that start with `#` are ignored. + +# app/ @commented-rule + +# Specify a default Code Owner by using a wildcard: +* @default-codeowner + +# Specify multiple Code Owners by using a tab or space: +* @multiple @code @owners + +# Rules defined later in the file take precedence over the rules +# defined before. +# For example, for all files with a filename ending in `.rb`: +*.rb @ruby-owner + +# Files with a `#` can still be accessed by escaping the pound sign: +\#file_with_pound.rb @owner-file-with-pound + +# Specify multiple Code Owners separated by spaces or tabs. +# In the following case the CODEOWNERS file from the root of the repo +# has 3 Code Owners (@multiple @code @owners): +CODEOWNERS @multiple @code @owners + +# You can use both usernames or email addresses to match +# users. Everything else is ignored. For example, this code +# specifies the `@legal` and a user with email `janedoe@gitlab.com` as the +# owner for the LICENSE file: +LICENSE @legal this_does_not_match janedoe@gitlab.com + +# Use group names to match groups, and nested groups to specify +# them as owners for a file: +README @group @group/with-nested/subgroup + +# End a path in a `/` to specify the Code Owners for every file +# nested in that directory, on any level: +/docs/ @all-docs + +# End a path in `/*` to specify Code Owners for every file in +# a directory, but not nested deeper. This code matches +# `docs/index.md` but not `docs/projects/index.md`: +/docs/* @root-docs + +# Include `/**` to specify Code Owners for all subdirectories +# in a directory. This rule matches `docs/projects/index.md` or +# `docs/development/index.md` +/docs/**/*.md @root-docs + +# This code makes matches a `lib` directory nested anywhere in the repository: +lib/ @lib-owner + +# This code match only a `config` directory in the root of the repository: +/config/ @config-owner + +# If the path contains spaces, escape them like this: +path\ with\ spaces/ @space-owner + +# Code Owners section: +[Documentation] +ee/docs @docs +docs @docs + +# Use of default owners for a section. In this case, all files (*) are owned by +# the dev team except the README.md and data-models which are owned by other teams. +[Development] @dev-team +* +README.md @docs-team +data-models/ @data-science-team + +# This section is combined with the previously defined [Documentation] section: +[DOCUMENTATION] +README.md @docs + +# Required section +[Section name] + +# Optional section +^[Section name] + +# Section requiring 5 approvals +[Section name][5] + +# Section with @username as default owner +[Section name] @username + +# Section with @group and @subgroup as default owners and requiring 2 approvals +[Section name][2] @group @subgroup