Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#431 Recognizers often ascribe wrong file type #455

Merged
merged 1 commit into from
Aug 14, 2018
Merged

Conversation

tfesenko
Copy link
Member

Fix contentType for an empty YAML file, now it returns a more generic
YEdit editor.
Why this fix works

  1. An empty file always returns INDETERMINATE as description. Note
    that it's not the VALID which is returned when a content describer
    actually accepts the input. See
    com.reprezen.swagedit.core.editor.TextContentDescriber.describe(InputStream, IContentDescription):
        String content = CharStreams.toString(new
InputStreamReader(contents));
        if (content.trim().isEmpty()) {
            return INDETERMINATE;
        }

        return isSupported(content) ? VALID : INVALID;
  1. All ContentDescriber for a given file extensions are equally
    important.
    Because all the ContentDescriber return INDETERMINATE, they are
    treated equally important, see
    org.eclipse.core.internal.content.ContentTypeCatalog.collectMatchingByContents(int, IContentType[], List<ContentType>, ILazySource, Map<String, Object>)

  2. Then IContentType are sorted and the first one wins
    org.eclipse.core.internal.content.ContentTypeCatalog.internalFindContentTypesFor(ILazySource, IContentType[][], Comparator<IContentType>, Comparator<IContentType>)
    sorts the inputs. In case of INDETERMINATE,
    ContentTypeCatalog.policyConstantGeneralIsBetter is used which is...

A sorting policy where the more generic content type wins.
Lexicographical comparison is done as a last resort when all other
criteria fail.

where more generic content type means, getDepth() is based on the
hierarchy defined by the base-type:

// first criteria: depth - the lower, the better
int depthCriteria = type1.getDepth() - type2.getDepth();

By using YEdit as the base-type, we make the YEdit the more generic
content type => it will be used in case of empty files.

Fix contentType for an empty YAML file, now it returns a more generic
YEdit editor.
Why this fix works

1. An empty file always returns `INDETERMINATE` as description. Note
that it's not the `VALID` which is returned when a content describer
actually accepts the input. See
`com.reprezen.swagedit.core.editor.TextContentDescriber.describe(InputStream,
IContentDescription)`:
```
        String content = CharStreams.toString(new
InputStreamReader(contents));
        if (content.trim().isEmpty()) {
            return INDETERMINATE;
        }

        return isSupported(content) ? VALID : INVALID;
```
2. All ContentDescriber for a given file extensions are equally
important.
Because all the ContentDescriber return `INDETERMINATE`, they are
treated equally important, see
`org.eclipse.core.internal.content.ContentTypeCatalog.collectMatchingByContents(int,
IContentType[], List<ContentType>, ILazySource, Map<String, Object>)`

3. Then `IContentType` are sorted and the first one wins
`org.eclipse.core.internal.content.ContentTypeCatalog.internalFindContentTypesFor(ILazySource,
IContentType[][], Comparator<IContentType>, Comparator<IContentType>)`
sorts the inputs. In case of `INDETERMINATE`,
`ContentTypeCatalog.policyConstantGeneralIsBetter` is used which is...
> A sorting policy where the more generic content type wins.
Lexicographical comparison is done as a last resort when all other
criteria fail.

where more generic content type means, `getDepth()` is based on the
hierarchy defined by the `base-type`:
```
// first criteria: depth - the lower, the better
int depthCriteria = type1.getDepth() - type2.getDepth();
```

--------

By using YEdit as the `base-type`, we make the YEdit the more generic
content type => it will be used in case of empty files.
Copy link
Member

@ghillairet ghillairet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passed QA

@ghillairet ghillairet merged commit ccb2eb5 into master Aug 14, 2018
@ghillairet ghillairet deleted the task/431 branch August 14, 2018 15:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants