Skip to content

Commit

Permalink
refact: rename IGrammarRegistryManager#getGrammarForFileType
Browse files Browse the repository at this point in the history
  • Loading branch information
sebthom committed Dec 22, 2023
1 parent 3db6c98 commit 5bde2a6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,26 @@ public interface IGrammarRegistryManager {
// --------------- TextMate grammar definitions methods

/**
* Returns the list of registered TextMate grammar definitions.
*
* @return the list of registered TextMate grammar definitions.
*/
IGrammarDefinition[] getDefinitions();

/**
* Add grammar definition to the registry.
*
* NOTE: you must call save() method if you wish to save in the preferences.
* <p/>
* <b>NOTE:</b> you must call {@link #save()} method to make the changes persistent.
*/
void registerGrammarDefinition(IGrammarDefinition definition);

/**
* Remove grammar definition from the registry.
*
* NOTE: you must call save() method if you wish to save in the preferences.
* <p/>
* <b>NOTE:</b> you must call {@link #save()} method to make the changes persistent.
*/
void unregisterGrammarDefinition(IGrammarDefinition definition);

/**
* Save the grammar definitions.
*
* @throws BackingStoreException
* Save the grammar definitions
*/
void save() throws BackingStoreException;

Expand All @@ -61,40 +57,33 @@ public interface IGrammarRegistryManager {
/**
* @param contentTypes the content types to lookup for grammar association.
*
* @return the first {@link IGrammar} that applies to given content-types, or
* <code>null</code> if no content-type has a grammar associated. Grammars associated
* with parent content-types will be returned if applicable.
* @return the first {@link IGrammar} that applies to given content-types, or <code>null</code> if no content-type
* has a grammar associated. Grammars associated with parent content-types will be returned if applicable.
*/
@Nullable
IGrammar getGrammarFor(IContentType @Nullable [] contentTypes);

/**
* Returns the {@link IGrammar} for the given scope name and null otherwise.
*
* @return the {@link IGrammar} for the given scope name and null otherwise.
*/
@Nullable
IGrammar getGrammarForScope(String scopeName);

/**
* Returns the {@link IGrammar} for the given file type and null otherwise.
* @param fileType a file extension
*
* @return the {@link IGrammar} for the file type name and null otherwise.
*/
@Nullable
IGrammar getGrammarForFileType(String fileType);
IGrammar getGrammarForFileExtension(String fileExtension);

/**
* Returns the list of content types bound with the given scope name and null otherwise.
*
* @return the list of content types bound with the given scope name and null otherwise.
*/
@Nullable
List<IContentType> getContentTypesForScope(String scopeName);

/**
* Returns list of scope names to inject for the given <code>scopeName</code> and null otherwise.
*
* @return list of scope names to inject for the given <code>scopeName</code> and null otherwise.
*/
@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,12 @@ public IGrammar getGrammarForScope(final String scopeName) {

@Nullable
@Override
public IGrammar getGrammarForFileType(String fileType) {
public IGrammar getGrammarForFileExtension(String fileExtension) {
// TODO: cache grammar by file types
final IGrammarDefinition[] definitions = getDefinitions();
// #202
if (fileType.startsWith(".")) {
fileType = fileType.substring(1);
if (fileExtension.startsWith(".")) {
fileExtension = fileExtension.substring(1);
}
for (final var definition : definitions) {
// Not very optimized because it forces the load of the whole grammar.
Expand All @@ -143,7 +143,7 @@ public IGrammar getGrammarForFileType(String fileType) {
final var grammar = getGrammarForScope(definition.getScopeName());
if (grammar != null) {
final Collection<String> fileTypes = grammar.getFileTypes();
if (fileTypes.contains(fileType)) {
if (fileTypes.contains(fileExtension)) {
return grammar;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,9 @@ IRegion getRegionOfTextEvent(final TextEvent event) {
// try to determine the grammar based on the file type
final String fileName = info.getFileName();
if (fileName.indexOf('.') > -1) {
final String fileType = new Path(fileName).getFileExtension();
if (fileType != null) {
grammar = TMEclipseRegistryPlugin.getGrammarRegistryManager().getGrammarForFileType(fileType);
final String fileExtension = new Path(fileName).getFileExtension();
if (fileExtension != null) {
grammar = TMEclipseRegistryPlugin.getGrammarRegistryManager().getGrammarForFileExtension(fileExtension);
}
}
}
Expand Down

0 comments on commit 5bde2a6

Please sign in to comment.