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

Add editorFileExtension when clause context #34889

Merged
merged 4 commits into from
Oct 10, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/vs/editor/common/editorContextKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export namespace EditorContextKeys {

// -- mode context keys
export const languageId = new RawContextKey<string>('editorLangId', undefined);
export const editorExtension = new RawContextKey<string>('editorExtension', undefined);
Copy link
Member

Choose a reason for hiding this comment

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

After a second look: we actually don't need to repeat this. Having resourceExtname/Extension is enough has it gets inherited to the editor from its column/slot. The reason for having editorLangId and resourceLangId is that latter is derived from the name while the former is more correct (look at the name and the contents)

export const hasCompletionItemProvider = new RawContextKey<boolean>('editorHasCompletionItemProvider', undefined);
export const hasCodeActionsProvider = new RawContextKey<boolean>('editorHasCodeActionsProvider', undefined);
export const hasCodeLensProvider = new RawContextKey<boolean>('editorHasCodeLensProvider', undefined);
Expand Down
5 changes: 5 additions & 0 deletions src/vs/editor/common/modes/editorModeContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import * as modes from 'vs/editor/common/modes';
import { ICommonCodeEditor } from 'vs/editor/common/editorCommon';
import { EditorContextKeys } from 'vs/editor/common/editorContextKeys';
import { Schemas } from 'vs/base/common/network';
import * as paths from 'vs/base/common/paths';

export class EditorModeContext extends Disposable {

private _editor: ICommonCodeEditor;

private _langId: IContextKey<string>;
private _editorExtension: IContextKey<string>;
private _hasCompletionItemProvider: IContextKey<boolean>;
private _hasCodeActionsProvider: IContextKey<boolean>;
private _hasCodeLensProvider: IContextKey<boolean>;
Expand All @@ -40,6 +42,7 @@ export class EditorModeContext extends Disposable {
this._editor = editor;

this._langId = EditorContextKeys.languageId.bindTo(contextKeyService);
this._editorExtension = EditorContextKeys.editorExtension.bindTo(contextKeyService);
this._hasCompletionItemProvider = EditorContextKeys.hasCompletionItemProvider.bindTo(contextKeyService);
this._hasCodeActionsProvider = EditorContextKeys.hasCodeActionsProvider.bindTo(contextKeyService);
this._hasCodeLensProvider = EditorContextKeys.hasCodeLensProvider.bindTo(contextKeyService);
Expand Down Expand Up @@ -87,6 +90,7 @@ export class EditorModeContext extends Disposable {

reset() {
this._langId.reset();
this._editorExtension.reset();
this._hasCompletionItemProvider.reset();
this._hasCodeActionsProvider.reset();
this._hasCodeLensProvider.reset();
Expand All @@ -111,6 +115,7 @@ export class EditorModeContext extends Disposable {
return;
}
this._langId.set(model.getLanguageIdentifier().language);
this._editorExtension.set(paths.extname(model.uri.fsPath));
this._hasCompletionItemProvider.set(modes.SuggestRegistry.has(model));
this._hasCodeActionsProvider.set(modes.CodeActionProviderRegistry.has(model));
this._hasCodeLensProvider.set(modes.CodeLensProviderRegistry.has(model));
Expand Down
5 changes: 5 additions & 0 deletions src/vs/workbench/common/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ export class ResourceContextKey implements IContextKey<URI> {
static Filename = new RawContextKey<string>('resourceFilename', undefined);
static LangId = new RawContextKey<string>('resourceLangId', undefined);
static Resource = new RawContextKey<URI>('resource', undefined);
static Extension = new RawContextKey<string>('resourceExtension', undefined);
Copy link
Member

Choose a reason for hiding this comment

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

So, I'd prefer resourceExtname. What do you think?


private _resourceKey: IContextKey<URI>;
private _schemeKey: IContextKey<string>;
private _filenameKey: IContextKey<string>;
private _langIdKey: IContextKey<string>;
private _extensionKey: IContextKey<string>;

constructor(
@IContextKeyService contextKeyService: IContextKeyService,
Expand All @@ -37,19 +39,22 @@ export class ResourceContextKey implements IContextKey<URI> {
this._filenameKey = ResourceContextKey.Filename.bindTo(contextKeyService);
this._langIdKey = ResourceContextKey.LangId.bindTo(contextKeyService);
this._resourceKey = ResourceContextKey.Resource.bindTo(contextKeyService);
this._extensionKey = ResourceContextKey.Extension.bindTo(contextKeyService);
}

set(value: URI) {
this._resourceKey.set(value);
this._schemeKey.set(value && value.scheme);
this._filenameKey.set(value && basename(value.fsPath));
this._langIdKey.set(value && this._modeService.getModeIdByFilenameOrFirstLine(value.fsPath));
this._extensionKey.set(value && paths.extname(value.fsPath));
Copy link
Member

Choose a reason for hiding this comment

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

Way to go but we to think if folks expect .do from foo.do or do...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think typical behavior is to return extension with the dot. At least that's what most standard libraries I know do - Node's path.extname, .Net Framework Path.GetExtension, or Ruby's File.extname

Copy link
Contributor Author

Choose a reason for hiding this comment

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

More interesting question is if users expects it to be normalized to lower case (i.e if "ABC.FSX" should return ".FSX" or just ".fsx")

Copy link
Member

Choose a reason for hiding this comment

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

Good questions... I'd say we have it "as is". I can see how a "normalized" version can help but that applies to many values, e.g. filename

}

reset(): void {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jrieken. an reason why _filenameKey is not reseted here?

Copy link
Member

Choose a reason for hiding this comment

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

I'd say that is bug cc @bpasero

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

🙉

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should I add this missing reset in this PR?

this._schemeKey.reset();
this._langIdKey.reset();
this._resourceKey.reset();
this._extensionKey.reset();
}

public get(): URI {
Expand Down