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

Adding ns form to a newly created clojure file #844

Closed
zelark opened this issue Nov 13, 2020 · 7 comments
Closed

Adding ns form to a newly created clojure file #844

zelark opened this issue Nov 13, 2020 · 7 comments

Comments

@zelark
Copy link
Contributor

zelark commented Nov 13, 2020

Cursive and Emacs have this feature. It would be nice to have it in Calva too.

When I create a new clojure file, I'd like its ns form is created automatically. For example if a new file is src/my_app/core.clj, I'd expect to see (ns my-app.core) in it.

To do it we need to add a handler for workspace.onDidCreateFiles.
https://code.visualstudio.com/api/references/vscode-api#workspace

Also the vscode-java extension already supports similar feature for java files.

@zelark
Copy link
Contributor Author

zelark commented Nov 14, 2020

We need a way to collect all current source paths.

@bpringe
Copy link
Member

bpringe commented Nov 14, 2020

Why are all source paths needed?

@zelark
Copy link
Contributor Author

zelark commented Nov 16, 2020

Because to get ns form we need to build a relative path first.

function resolvePackageName(sourcePaths: string[], filePath: string): string {
    if (!sourcePaths || !sourcePaths.length) {
        return "";
    }
    for (const sourcePath of sourcePaths) {
        if (isPrefix(sourcePath, filePath)) {
            const relative = path.relative(sourcePath, path.dirname(filePath));
            return relative.replace(/[\/\\]/g, ".")
                           .replace("_", "-");
        }
    }
    return "";
}

In general, any directory under a clojure project could be a source path.

There are at least two common ways to get source paths.

  1. Read deps.edn or project.clj (Cursive)
  2. Ask nREPL for a classpath and extract source paths from it (CIDER, but also it has a fallback way).

@zelark
Copy link
Contributor Author

zelark commented Nov 16, 2020

Today I made a prototype with a hardcoded source path. Works fine.

@zelark
Copy link
Contributor Author

zelark commented Nov 16, 2020

ns-form-autocreation

@bpringe
Copy link
Member

bpringe commented Nov 16, 2020

Ah, so wherever the file is created you need to find the first parent folder that is a source directory (which probably would be the only in that path)?

@zelark
Copy link
Contributor Author

zelark commented Nov 16, 2020

Right.

@zelark zelark mentioned this issue Nov 20, 2020
22 tasks
@zelark zelark closed this as completed Nov 25, 2020
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