{
    "scopeName": "namespace.r",
    "fileTypes": [
        "NAMESPACE"
    ],
    "patterns": [
        {
            "include": "#directive-export"
        },
        {
            "include": "#directive-export-pattern"
        },
        {
            "include": "#directive-export-s3"
        },
        {
            "include": "#directive-import"
        },
        {
            "include": "#directive-import-dynlib"
        },
        {
            "include": "#directive-import-from"
        },
        {
            "include": "#comment"
        }
    ],
    "repository": {
        "arglist-highlight-all": {
            "patterns": [
                {
                    "match": "([^,\\s)]+|`[^`]+`)\\s*",
                    "captures":{
                        "1": { "name": "markup.underline" }
                    }
                }
            ]
        },
        "arglist-highlight-first": {
            "patterns": [
                {
                    "begin": "\\s*([^,\\s)]+|`[^`]+`)\\s*",
                    "beginCaptures":{
                        "1": { "name": "markup.underline" }
                    },
                    "end": "(?=\\))",
                    "patterns": [
                        {
                            "match": "\\s*([^,\\s)]+|`[^`]+`)\\s*",
                            "captures":{
                                "1": { "name": "variable.parameter" }
                            }
                        }
                    ]
                }
            ]
        },
        "arglist-highlight-not-first": {
            "patterns": [
                {
                    "begin": "\\s*([^,\\s)]+|`[^`]+`)\\s*",
                    "beginCaptures":{
                        "1": { "name": "variable.parameter" }
                    },
                    "end": "(?=\\))",
                    "patterns": [
                        {
                            "match": "\\s*([^,\\s)]+|`[^`]+`)\\s*",
                            "captures":{
                                "1": { "name": "markup.underline" }
                            }
                        }
                    ]
                }
            ]
        },
        "directive-export": {
            "patterns": [
                {
                    "begin": "^\\s*(export|exportClasses|exportMethods)\\s*(\\()",
                    "beginCaptures":{
                        "1": { "name": "variable.language" },
                        "2": { "name": "punctuation.section.parens.start" }
                    },
                    "contentName": "meta.function-call.arguments.r",
                    "end": "(\\))",
                    "endCaptures": {
                        "1": { "name": "punctuation.section.parens.end" }
                    },
                    "patterns": [
                        {
                            "include": "#arglist-highlight-all"
                        }
                    ]
                }
            ]
        },
        "directive-export-pattern": {
            "patterns": [
                {
                    "begin": "^\\s*(exportPattern|exportClassPattern)\\s*(\\()\\s*",
                    "beginCaptures":{
                        "1": { "name": "variable.language" },
                        "2": { "name": "punctuation.section.parens.start" }
                    },
                    "contentName": "meta.function-call.arguments.r",
                    "end": "\\s*(\\))",
                    "patterns": [
                        {
                            "comment": "unfortunately we can not just include source.js.regexp here as backticks need to be escaped",
                            "match": "\"[^\"]*\"",
                            "name": "string.quoted.double"
                        }
                    ]
                }
            ]
        },
        
        "directive-export-s3": {
            "patterns": [
                {
                    "begin": "^\\s*(S3method)\\s*(\\()",
                    "beginCaptures":{
                        "1": { "name": "variable.language" },
                        "2": { "name": "punctuation.section.parens.start" }
                    },
                    "end": "(\\))",
                    "endCaptures": {
                        "1": { "name": "punctuation.section.parens.end" }
                    },
                    "patterns": [
                        {
                            "include": "#arglist-highlight-first"
                        }
                    ]
                }
            ]
        },
        "directive-exports": {
            "patterns": [
                {
                    "match": "^\\s*(exportClasses|exportMethods|S3method)\\s*\\(([^\\)]*)\\s*\\)",
                    "captures":{
                        "1": { "name": "variable.language" },
                        "2": { "name": "markup.underline" }
                    }
                }
            ]
        },
        "directive-import": {
            "patterns": [
                {
                    "begin": "^\\s*(import)\\s*(\\()",
                    "beginCaptures":{
                        "1": { "name": "keyword.control" },
                        "2": { "name": "punctuation.section.parens.start" }
                    },
                    "end": "(:?\\s*,\\s*(except=c\\([^)]*\\)))?\\s*(\\))",
                    "endCaptures": {
                        "1": { "name": "variable.parameter" },
                        "2": { "name": "punctuation.section.parens.end" }
                    },
                    "patterns": [
                        {
                            "include": "#arglist-highlight-all"
                        }
                    ]
                }
            ]
        },
        "directive-import-dynlib": {
            "patterns": [
                {
                    "begin": "^\\s*(useDynLib)\\s*(\\()",
                    "beginCaptures":{
                        "1": { "name": "keyword.control" },
                        "2": { "name": "punctuation.section.parens.start" }
                    },
                    "end": "(\\))",
                    "endCaptures": {
                        "1": { "name": "punctuation.section.parens.end" }
                    },
                    "patterns": [
                        {
                            "include": "#arglist-highlight-first"
                        }
                    ]
                }
            ]
        },
        "directive-import-from": {
            "patterns": [
                {
                    "begin": "^\\s*(importFrom|importClassesFrom|importMethodsFrom)\\s*(\\()",
                    "beginCaptures":{
                        "1": { "name": "keyword.control" },
                        "2": { "name": "punctuation.section.parens.start" }
                    },
                    "end": "(\\))",
                    "endCaptures": {
                        "1": { "name": "punctuation.section.parens.end" }
                    },
                    "patterns": [
                        {
                            "include": "#arglist-highlight-not-first"
                        }
                    ]
                }
            ]
        },
        "comment": {
            "patterns": [
                {
                    "comment": "comment",
                    "match": "#[^$]*",
                    "name": "comment.line.number-sign"
                }
            ]
        }
    }
}
