1#include "basicScopesAttributeProvider.h"
7std::regex BasicScopeAttributesProvider::STANDARD_TOKEN_TYPE_REGEXP(
"\\b(comment|string|regex|meta\\.embedded)\\b");
13 : languageId(languageId_), tokenType(tokenType_) {
18template<
typename TValue>
19ScopeMatcher<TValue>::ScopeMatcher(
const std::vector<std::pair<std::string, TValue>>& valuesList)
20 : scopesRegExp(nullptr), hasValues(!valuesList.empty()) {
22 if (valuesList.empty()) {
26 for (
const auto& pair : valuesList) {
27 values[pair.first] = pair.second;
31 std::vector<std::string> escapedScopes;
32 for (
const auto& pair : valuesList) {
33 escapedScopes.push_back(escapeRegExpCharacters(pair.first));
36 std::sort(escapedScopes.begin(), escapedScopes.end());
37 std::reverse(escapedScopes.begin(), escapedScopes.end());
40 std::string pattern =
"^((";
41 for (
size_t i = 0; i < escapedScopes.size(); i++) {
42 if (i > 0) pattern +=
")|(";
43 pattern += escapedScopes[i];
45 pattern +=
"))($|\\.)";
47 scopesRegExp =
new std::regex(pattern);
50template<
typename TValue>
51ScopeMatcher<TValue>::~ScopeMatcher() {
55template<
typename TValue>
56TValue* ScopeMatcher<TValue>::match(
const ScopeName& scope) {
62 if (!std::regex_search(scope, m, *scopesRegExp)) {
66 std::string matchedScope = m[1].str();
67 auto it = values.find(matchedScope);
68 if (it != values.end()) {
76template class ScopeMatcher<int>;
80BasicScopeAttributesProvider::BasicScopeAttributesProvider(
81 int initialLanguageId,
82 const EmbeddedLanguagesMap* embeddedLanguages)
85 std::vector<std::pair<std::string, int>> embeddedLanguagesList;
86 if (embeddedLanguages) {
87 for (
const auto& pair : *embeddedLanguages) {
88 embeddedLanguagesList.push_back(pair);
92 _embeddedLanguagesMatcher =
new ScopeMatcher<int>(embeddedLanguagesList);
94 _getBasicScopeAttributesCache =
new CachedFn<ScopeName, BasicScopeAttributes>(
95 [
this](
const ScopeName& scopeName) {
96 int languageId = this->_scopeToLanguage(scopeName);
98 return BasicScopeAttributes(languageId, standardTokenType);
103BasicScopeAttributesProvider::~BasicScopeAttributesProvider() {
104 delete _embeddedLanguagesMatcher;
105 delete _getBasicScopeAttributesCache;
108BasicScopeAttributes BasicScopeAttributesProvider::getDefaultAttributes()
const {
109 return _defaultAttributes;
112BasicScopeAttributes BasicScopeAttributesProvider::getBasicScopeAttributes(
const ScopeName* scopeName) {
113 if (scopeName ==
nullptr) {
114 return NULL_SCOPE_METADATA;
116 return _getBasicScopeAttributesCache->get(*scopeName);
119int BasicScopeAttributesProvider::_scopeToLanguage(
const ScopeName& scope) {
120 int* languageId = _embeddedLanguagesMatcher->match(scope);
121 return languageId ? *languageId : 0;
126 if (!std::regex_search(scopeName, m, STANDARD_TOKEN_TYPE_REGEXP)) {
127 return OptionalStandardTokenType::NotSet;
130 std::string matchedType = m[1].str();
131 if (matchedType ==
"comment") {
132 return OptionalStandardTokenType::Comment;
133 }
else if (matchedType ==
"string") {
134 return OptionalStandardTokenType::String;
135 }
else if (matchedType ==
"regex") {
136 return OptionalStandardTokenType::RegEx;
137 }
else if (matchedType ==
"meta.embedded") {
138 return OptionalStandardTokenType::Other;
141 return OptionalStandardTokenType::NotSet;
OptionalStandardTokenType
Standard token type with optional (unknown) state.
@ Other
Not a recognized standard type.
@ NotSet
Type not determined or not applicable.