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

Rule Suggestion: Convert between Tabs and spaces #36

Closed
chrisgrieser opened this issue Sep 12, 2021 · 9 comments
Closed

Rule Suggestion: Convert between Tabs and spaces #36

chrisgrieser opened this issue Sep 12, 2021 · 9 comments

Comments

@chrisgrieser
Copy link
Contributor

chrisgrieser commented Sep 12, 2021

First of all, thanks for the plugin! It's one of those little things saving a bit of time every day.

A rule that I would find very useful is the conversion between tabs and spaces. For me personally, this would be incredibly useful because all the notes I imported from earlier apps use spaces, but the various relationship line css snippets need tabs to work.

@platers
Copy link
Owner

platers commented Sep 13, 2021

Would this logic work?

For every line, count the number of leading spaces, spaces. Replace them with spaces/tabsize tabs.

@chrisgrieser
Copy link
Contributor Author

I think so.

One consideration could be to also deal with cases where you have one more space than your tabsize, which would result in tab+space. But that would rather be another rule, I guess?

@chrisgrieser
Copy link
Contributor Author

chrisgrieser commented Sep 13, 2021

You can pass it in as an option, the user can set tabsize=2 in the settings. See the other rules for examples.

That would work, but it isn't ideal. Users might not consider that the tabsize set in the Linter plugin is independent from the tabsize set in the Obsidian main setting, leading to unnecessary confusion about the use of that rule

@platers
Copy link
Owner

platers commented Sep 13, 2021

I was thinking about your use case, when you are copying from external sources. It would be nice if the default tabsize was the obsidian tab size though.

@platers
Copy link
Owner

platers commented Sep 14, 2021

on the discord, found vault.getConfig("tabSize").

@chrisgrieser
Copy link
Contributor Author

chrisgrieser commented Sep 14, 2021

I have the rule ready, but I am a bit uncertain about the typescript part.

  1. is this how you would do it in the rules.ts?
(text: string) => {
        let tabsize = vault.getConfig("tabSize"); // <- this the proper syntax?
        (...)
},
  1. also, concerning the examples, how exactly do I have to write them, with the indention in the code of the rules.ts itself? or does the dedent already deal with that?

@platers
Copy link
Owner

platers commented Sep 16, 2021

You should test the tabSize syntax in obsidian. If you put the repo in your plugins folder, after you run npm run compile you should be able to activate it in obsidian and test it.

Yep dedent deals with it, its pretty smart about the indentation.

@chrisgrieser
Copy link
Contributor Author

chrisgrieser commented Sep 16, 2021

okay, I have a working solution ready, but there are two problem.


Problem 1: vault.getConfig("tabSize") does not work, when compiling, I get the following error:

(!) Plugin typescript: @rollup/plugin-typescript TS2304: Cannot find name 'vault'.
src/rules.ts: (654:23)

654         let tabsize = vault.getConfig("tabSize");
                          ~~~~~

I also tried adding import { App } from 'obsidian'; to the imports at the top of the document, but that did not solve it.


Problem 2: So what I did for now, was to simply use tabsize as an option, as you suggested before. npm run compile works fine and all tests are passed, however, npm run lint has – surprise – a problems with me having added tabs.

> obsidian-sample-plugin@0.12.0 lint
> eslint . --ext .ts --fix

/Users/chrisgrieser/Google Drive/File Hub/obsidian-linter/src/rules.ts
  674:13  error  Unexpected tab character  no-tabs
  676:13  error  Unexpected tab character  no-tabs

✖ 2 problems (2 errors, 0 warnings)

674:13 and 674:13 are, of course, exactly the two positions where I have inserted the tabs for the example. Naturally, removing the tabs for the linter causes the test not to pass. 😝

So assume you would have to change some linter configuration before I can commit this rule?

@chrisgrieser
Copy link
Contributor Author

Well, at least problem #2 I figured out on my own by using inline comments to temporarily disable linting rules. Will be adding a PR shortly.

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