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

using GetDefinitionForFilename incrementally #30

Closed
mvdan opened this issue Sep 28, 2019 · 5 comments
Closed

using GetDefinitionForFilename incrementally #30

mvdan opened this issue Sep 28, 2019 · 5 comments

Comments

@mvdan
Copy link
Contributor

mvdan commented Sep 28, 2019

Thanks for this library! I'm looking at using it to implement mvdan/sh#393.

Here's one problem, though. When one runs a command like shfmt -l -w ., it searches all shell files under the current directory, similar to what gofmt would do.

Imagine a fairly simple use case, where we have a few hundred shell files and an .editorconfig in the current directory. As I read the source for GetDefinitionForFilename, I'm a bit horrified, as using it for each of the shell script paths would result in the library re-reading and re-parsing the editorconfig hundreds of times.

I think the library should expose a stateful, incremental API. Keeping data in a struct, and using methods, it could cache:

  • which directories are known to contain (or not contain) an editorconfig
  • editorconfig files that have already been parsed

For my sample use case above, this would mean only parsing the editorconfig once, and also only one stat call per directory containing a shell script.

For the sake of simplicity, merging of editorconfig files doesn't need to be cached.

@mvdan
Copy link
Contributor Author

mvdan commented Sep 28, 2019

If this sounds like a good idea and noone plans to work on it, I might send a patch myself.

@mvdan
Copy link
Contributor Author

mvdan commented Sep 28, 2019

Here's a draft of what the API could look like:

type IncrementalFinder struct {
    ConfigName string

    // unexported fields
    configsByDir map[string]*EditorConfig
}

func (*IncrementalFinder) DefinitionForFilename(filename string) (*EditorConfig, error)

@andreynering
Copy link
Contributor

Hi @mvdan,

This suggestion is totally reasonable.

My counter-proposal on the API would be something like:

type Finder {
	// unexported fields only
}

func New(r io.Reader) (*Finder, error) {
	// ...
}

func (*Finder) Find(filename string) (*EditorConfig, error) {
	// ...
}

@mvdan
Copy link
Contributor Author

mvdan commented Sep 30, 2019

I don't think that API would cut it; it's constrained to a single io.Reader, whereas I want to cache multiple parsed editorconfig files at once.

@andreynering
Copy link
Contributor

Closing this since you're making your own library for this anyway.

I don't think we'll have that soon in this one.

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

No branches or pull requests

2 participants