Allow using path suffixes to associate language file-types (#2455)

* feat(syntax): add strategy to associate file to language through pattern

File path will match if it ends with any of the file types provided in the config.
Also used this feature to add support for the .git/config and .ssh/config files

* Add /etc/ssh/ssh_config to languages.toml

* cargo xtask docgen

* Update languages.md

* Update languages.md

* Update book/src/languages.md

Co-authored-by: Ivan Tham <pickfire@riseup.net>

* Update book/src/languages.md

Co-authored-by: Ivan Tham <pickfire@riseup.net>

Co-authored-by: Ivan Tham <pickfire@riseup.net>
pull/4384/head
midnightexigent 2 years ago committed by GitHub
parent 78c0cdc519
commit d801a6693c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -50,7 +50,7 @@ These configuration keys are available:
| `name` | The name of the language | | `name` | The name of the language |
| `scope` | A string like `source.js` that identifies the language. Currently, we strive to match the scope names used by popular TextMate grammars and by the Linguist library. Usually `source.<name>` or `text.<name>` in case of markup languages | | `scope` | A string like `source.js` that identifies the language. Currently, we strive to match the scope names used by popular TextMate grammars and by the Linguist library. Usually `source.<name>` or `text.<name>` in case of markup languages |
| `injection-regex` | regex pattern that will be tested against a language name in order to determine whether this language should be used for a potential [language injection][treesitter-language-injection] site. | | `injection-regex` | regex pattern that will be tested against a language name in order to determine whether this language should be used for a potential [language injection][treesitter-language-injection] site. |
| `file-types` | The filetypes of the language, for example `["yml", "yaml"]`. Extensions and full file names are supported. | | `file-types` | The filetypes of the language, for example `["yml", "yaml"]`. This attempts to match by exact file name (`.zshrc`), then by file extension (`toml`), then by path suffix (`.git/config`). |
| `shebangs` | The interpreters from the shebang line, for example `["sh", "bash"]` | | `shebangs` | The interpreters from the shebang line, for example `["sh", "bash"]` |
| `roots` | A set of marker files to look for when trying to find the workspace root. For example `Cargo.lock`, `yarn.lock` | | `roots` | A set of marker files to look for when trying to find the workspace root. For example `Cargo.lock`, `yarn.lock` |
| `auto-format` | Whether to autoformat this language when saving | | `auto-format` | Whether to autoformat this language when saving |

@ -471,9 +471,10 @@ impl Loader {
for file_type in &config.file_types { for file_type in &config.file_types {
// entry().or_insert(Vec::new).push(language_id); // entry().or_insert(Vec::new).push(language_id);
let file_type = file_type.replace('/', &std::path::MAIN_SEPARATOR.to_string());
loader loader
.language_config_ids_by_file_type .language_config_ids_by_file_type
.insert(file_type.clone(), language_id); .insert(file_type, language_id);
} }
for shebang in &config.shebangs { for shebang in &config.shebangs {
loader loader
@ -498,6 +499,17 @@ impl Loader {
path.extension() path.extension()
.and_then(|extension| extension.to_str()) .and_then(|extension| extension.to_str())
.and_then(|extension| self.language_config_ids_by_file_type.get(extension)) .and_then(|extension| self.language_config_ids_by_file_type.get(extension))
})
.or_else(|| {
self.language_config_ids_by_file_type
.iter()
.find_map(|(file_type, id)| {
if path.to_str()?.ends_with(file_type) {
Some(id)
} else {
None
}
})
}); });
configuration_id.and_then(|&id| self.language_configs.get(id).cloned()) configuration_id.and_then(|&id| self.language_configs.get(id).cloned())

@ -1054,7 +1054,7 @@ name = "git-config"
scope = "source.gitconfig" scope = "source.gitconfig"
roots = [] roots = []
# TODO: allow specifying file-types as a regex so we can read directory names (e.g. `.git/config`) # TODO: allow specifying file-types as a regex so we can read directory names (e.g. `.git/config`)
file-types = [".gitmodules", ".gitconfig"] file-types = [".gitmodules", ".gitconfig", ".git/config", ".config/git/config"]
injection-regex = "git-config" injection-regex = "git-config"
comment-token = "#" comment-token = "#"
indent = { tab-width = 4, unit = "\t" } indent = { tab-width = 4, unit = "\t" }

Loading…
Cancel
Save