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

Introduce IRelativePattern and adopt in DocumentFilter/FileWatcher/FileSearch #34695

Merged
merged 15 commits into from
Sep 22, 2017

Conversation

bpasero
Copy link
Member

@bpasero bpasero commented Sep 20, 2017

Refs: #34157

Creating this for discussion. Idea is to make it easier for extensions to work with patterns that should be relative to the workspace. We typically ask for a string for the pattern and now we could add an OR-type for a workspace relative pattern described by WorkspacePattern. The pattern will be matched against the provided base folder and can be relative (e.g. not starting with the workspace path or **).

export interface DocumentFilter {

	/**
	 * A language id, like `typescript`.
	 */
	language?: string;

	/**
	 * A Uri [scheme](#Uri.scheme), like `file` or `untitled`.
	 */
	scheme?: string;

	/**
	 * A glob pattern, like `*.{ts,js}`.
	 */
	pattern?: string | WorkspacePattern;
}

export interface WorkspacePattern {
	base: WorkspaceFolder;
	pattern: string;
}

Before doing more polish, @jrieken @dbaeumer for initial input.

@bpasero bpasero self-assigned this Sep 20, 2017
@bpasero bpasero added this to the On Deck milestone Sep 20, 2017
pattern?: string | WorkspacePattern;
}

export interface WorkspacePattern {
Copy link
Member

Choose a reason for hiding this comment

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

jsdoc missing

Copy link
Member

Choose a reason for hiding this comment

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

I wonder if we should also make it a relative pattern as we have in the lower level. A class like this

class RelativePattern {
  base: Uri;
  pattern: string;

  constructor(pattern:string, base: WorkspaceFolder | Uri)
}

The ctor adds some util-sugar to it and making just a Uri allows for more generic use in extensions, e.g watch for file event in a certain folder (unrelated to a workspace folder)

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah I like that 👍

Copy link
Member

Choose a reason for hiding this comment

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

In addition I would add the type to createFileSystemWatcher. Eg. globPattern: string | RelativePattern

@bpasero
Copy link
Member Author

bpasero commented Sep 20, 2017

@jrieken @dbaeumer latest changes include a new RelativePattern type as suggested. I think one challenge is that I cannot make this proposed API (at least I could not make it).

@dbaeumer
Copy link
Member

Added my comments


export function toLanguageSelector(selector: vscode.DocumentSelector): LanguageSelector {
return selector as LanguageSelector;
}
Copy link
Member

Choose a reason for hiding this comment

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

Do we actually need this? Is that an URI issue?

Copy link
Member Author

Choose a reason for hiding this comment

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

@jrieken yeah I think so, now I have to do the same for the watcher:

image

Copy link
Member

Choose a reason for hiding this comment

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

Try again, I have pushed some changes to URI and it should not be an issue anymore

@bpasero
Copy link
Member Author

bpasero commented Sep 21, 2017

@dbaeumer @jrieken yeah I like adding this to the file watcher too because we have an actual use case for @joaomoreno who needs to watch per repository. And now with being able to use any URI as base, this should be possible.

I added it via 837ef11

@bpasero bpasero modified the milestones: On Deck, September 2017 Sep 21, 2017
@bpasero bpasero changed the title Introduce IRelativePattern and adopt in DocumentFilter Introduce IRelativePattern and adopt in DocumentFilter/FileWatcher Sep 21, 2017
@joaomoreno
Copy link
Member

joaomoreno commented Sep 21, 2017

That's pretty cool. And I do believe that's how globs are supposed to work anyway.

I don't understand why we ever had globs which matched to absolute values. You'll find this pretty much nowhere else. In gulp, globs are always matched to a base. In zsh, they are always matched against the cwd, unless they start with /.

@bpasero
Copy link
Member Author

bpasero commented Sep 22, 2017

@jrieken @dbaeumer I also decided to adopt this for findFiles to make this consistent and I have a use case for this from adopting the vscode-docker extension. I was also able to get rid of the no-op type conversions after merging latest from master in thanks to the URI changes.

@bpasero bpasero changed the title Introduce IRelativePattern and adopt in DocumentFilter/FileWatcher Introduce IRelativePattern and adopt in DocumentFilter/FileWatcher/FileSearch Sep 22, 2017

export function toLanguageSelector(selector: vscode.DocumentSelector): LanguageSelector {
return selector as LanguageSelector;
}
Copy link
Member

Choose a reason for hiding this comment

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

Try again, I have pushed some changes to URI and it should not be an issue anymore

/**
* A relative glob pattern like `*.{ts,js}`.
*/
readonly pattern: string;
Copy link
Member

Choose a reason for hiding this comment

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

Maybe no readonly for those two?

Copy link
Member Author

Choose a reason for hiding this comment

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

@jrieken yeah, pushed

/**
* A base to which the pattern will be matched against relatively.
*/
readonly base: Uri;
Copy link
Member

Choose a reason for hiding this comment

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

On a second thought this could also just be a string, right? We have the sugar ctor but technically we aren't globbing against a URI but against it path/fspath?

Copy link
Member Author

@bpasero bpasero Sep 22, 2017

Choose a reason for hiding this comment

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

@jrieken yes it should be a string because this is about file paths, I changed that via f370693

* @param maxResults An upper-bound for the result.
* @param token A token that can be used to signal cancellation to the underlying search engine.
* @return A thenable that resolves to an array of resource identifiers.
*/
export function findFiles(include: string, exclude?: string, maxResults?: number, token?: CancellationToken): Thenable<Uri[]>;
export function findFiles(include: string | RelativePattern, exclude?: string | RelativePattern, maxResults?: number, token?: CancellationToken): Thenable<Uri[]>;
Copy link
Member

Choose a reason for hiding this comment

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

To avoid repetition we could consider something like

/**
 * Write Jsdoc once
 */
type GlobPattern = string | RelativePattern

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 Author

Choose a reason for hiding this comment

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

@jrieken done via 0ba6fb6

export class RelativePattern implements IRelativePattern {
base: string;

constructor(public pattern: string, base: vscode.WorkspaceFolder | string) {
Copy link
Member

@jrieken jrieken Sep 22, 2017

Choose a reason for hiding this comment

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

fugly - mixing properties definitions like that

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, oversight, fixed via 328f82a

@bpasero
Copy link
Member Author

bpasero commented Sep 22, 2017

@roblourens it seems to me that the include/exclude patterns for findFiles actually are always matched in a relative way against the workspace they are coming from. E.g. I am seeing results coming back when passing in *.js, however I am not seeing results coming in when prefixing this pattern with the absolute path (e.g. /Users/bpasero/dev/myProject/*.js).

Given that, it seems we have a slight inconsistency with how we handle patterns in the API:

  • file watcher: absolute
  • document selector: absolute
  • search: relative to workspace

This makes it a harder to use the GlobPattern type for findFiles. It almost seems to me that we need something like:

type RelativePattern = string | RelativePatternWithBase

and then change findFiles method to only accept this RelativePattern type. It somewhat makes sense because a relative pattern can always also just be a string and if that is true we should match it against the workspace root(s).

@dbaeumer @jrieken thoughts?

Copy link
Member

@jrieken jrieken left a comment

Choose a reason for hiding this comment

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

I am happy with the API changes, the rest I didn't really look at

@bpasero bpasero merged commit 3e9fa59 into master Sep 22, 2017
@bpasero bpasero deleted the ben/34157 branch September 22, 2017 11:52
@bpasero
Copy link
Member Author

bpasero commented Sep 22, 2017

I clarified this in the JSDocs how patterns apply depending on where they are used, I think we should not make this any more complicated by introducing even more types. The reality is that string can always either be a relative glob pattern or an absolute glob pattern, we cannot enforce one or the other.

@github-actions github-actions bot locked and limited conversation to collaborators Mar 27, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants