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

format: fix indentation for hotkeys {:: and }:: #12

Closed
wants to merge 3 commits into from

Conversation

FuPeiJiang
Copy link

@FuPeiJiang FuPeiJiang commented Dec 4, 2020

having {:: or }:: as hotkey breaks the indentation...
you can reproduce issue with \format_test_cases\{} match testing.ahk

3 commits from fade2gray (Nov 13, 2020)
https://github.com/fade2gray/vscode-autohotkey-plus-plus/tree/master

grammar: createEditorListenr->createEditorListener
please do not use regex when not necessary

added purity_greedy in codeUtil.ts
@mark-wiemer mark-wiemer self-requested a review December 13, 2020 19:30
@mark-wiemer mark-wiemer self-assigned this Dec 13, 2020
@mark-wiemer mark-wiemer changed the base branch from master to dev December 13, 2020 22:15
Copy link
Member

@mark-wiemer mark-wiemer left a comment

Choose a reason for hiding this comment

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

Although I'm not super familiar with formattingProvider.ts yet, I'm comfortable merging to dev once you make the requested changes. The "If you like" comments are bonus points 😄

.vscode/settings.json Show resolved Hide resolved
format_test_cases/{} match testing.ahk Show resolved Hide resolved
package.json Show resolved Hide resolved
@@ -55,7 +55,8 @@ export class FormatProvider implements vscode.DocumentFormattingEditProvider {
}
continue;
}
const purityText = CodeUtil.purity(originText.toLowerCase());
const purityText = CodeUtil.purity_greedy(originText.toLowerCase());
// console.log(purityText)
Copy link
Member

Choose a reason for hiding this comment

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

Remove this commented out console log

if (purityText.match(/\b(return)\b/i) && tagDeep === deep) {
tagDeep == 0;
tagDeep == 0; //this does nothing
Copy link
Member

Choose a reason for hiding this comment

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

If this line does nothing, then remove it. If we cannot verify whether it does anything, we should leave it in with a // TODO: does this do anything? comment

Comment on lines +6 to +18
public static purity_greedy(origin: string): string {
if (!origin) return '';
// TODO: untest
return origin
.replace(/;.+/, '')
.replace(/".*?"/g, '')
.replace(/\{.*\}/g, '')
.replace(/ +/g, ' ')
.replace(/\bgui\b.*/gi, '')
.replace(/\b(msgbox)\b.+?%/gi, '$1');
}

public static purity(origin: string, greedy: boolean = false): string {
Copy link
Member

Choose a reason for hiding this comment

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

No need to make this a new function, just remove the unused greedy parameter from purity instead and call that. I see you also remove some unnecessary ? matchers, feel free to remove those from the original purity as well.

Copy link
Author

Choose a reason for hiding this comment

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

I added that greedy parameter, and decided against it, because this is called EVERY line, and if statement is expensive,

what would be cool would be to replace purity with purity_greedy everywhere, but idk if it's a breaking change

I forgot to remove that unused parameter

Copy link
Member

Choose a reason for hiding this comment

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

It shouldn't be a breaking change. What is the difference between purity_greedy and purity? I see none. Just use purity since we now have two functions doing the same thing.

Copy link
Author

Choose a reason for hiding this comment

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

it really isn't clear. it doesn't even show it.
line 12
.replace(/\{.*\}/g, '')
line 24
.replace(/\{.*?\}/g, '')

Copy link
Member

Choose a reason for hiding this comment

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

I noticed that. Removing the ? from the regex does make it appear simpler, but they have the same functionality. Since the ? follows a *, it never does anything. The * following the . captures 0 or more instances of any char and the ? captures 0 or 1 instance(s) of any char. With or without the ?, the regex captures 0 or more instances of any char.

If I'm wrong, please provide a counterexample. For simplicity, consider be*?d and note that it captures:

  • bd
  • bed
  • beed

Now consider be*d. It captures the exact same things.

Copy link
Member

@mark-wiemer mark-wiemer Jan 5, 2021

Choose a reason for hiding this comment

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

I recommend you go ahead and replace all *? with * for simplicity. I can manually test the functionalities that depend on purity if you don't feel comfortable doing it. I still don't see any cause for having both purity_greedy and purity since they do the same thing.

Copy link
Author

Choose a reason for hiding this comment

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

regex lesson: normally ? means 0 or 1 instance(s), but when ? is placed after *, it transforms the * into lazy, because * is greedy by Default

you can use https://regexr.com/ or https://regex101.com/ to help understand regex.

counterexample (use one of the 2 websites), the RegExp is code and line below RegExp is the text string:
\{.*\}
fwefwefwef{very greedy } more cake}wefwefwefwef

\{.*?\}
fwefwefwef{nope, I've had enough } more cake}wefwefwefwef


I will have to find and understand the other use cases

Copy link
Member

Choose a reason for hiding this comment

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

Gotcha. Then of course don't change the regex! I will say, this PR is a bit stale. I am looking into fixing the last January bug now (#18), hopefully I will learn a bit about the formatter

src/provider/formattingProvider.ts Show resolved Hide resolved
deep -= temp;
if (temp > 0) {
notDeep = false;
if (purityText.includes("}")) {
Copy link
Member

Choose a reason for hiding this comment

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

Wrap strings in single quotes, not double quotes, throughout.

e.g. if (purityText.includes('}')) {

Copy link
Author

Choose a reason for hiding this comment

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

suggestion: add eslint to devDependencies and in .vscode/settings.json

  "editor.codeActionsOnSave": {
    "source.fixAll.eslint": true
  },

Copy link
Member

Choose a reason for hiding this comment

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

Sure, we can do that in another PR. ESLint is on the backlog. For now, would you mind changing them manually? If not, I suppose I can still merge the PR.

.replace(/\b(msgbox)\b.+?%/gi, '$1');
}

public static purity(origin: string, greedy: boolean = false): string {
if (!origin) return '';
// TODO: untest
Copy link
Member

Choose a reason for hiding this comment

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

If you'd like, I'd appreciate removing this comment, I don't know what it means.

}
notDeep = false;
}

if (oneCommandCode && purityText.match(/{/) != null) {
Copy link
Member

Choose a reason for hiding this comment

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

If you'd like, you can remove this use of regex: ... && purityText.includes('{')) {

@FuPeiJiang
Copy link
Author

I can remove "unwanted" stuff.
But, I want to keep some "unwanted" stuff in my personal branch, (for example, closure compiler.ahk)

I don't want to remove my stuff EVERY TIME I want to merge here.
I need a way to make a branch, based off my personal branch, but subtracted from the "unwanted" stuff
so the unwanted stuff will be automatically removed every time I "subtract-merge" into the pull request branch

the unwanted stuff can even be lines, or EVEN words, in file
so, not restricted to file subtraction.

Is there already a way to do this ?
if no, I will try to make something

@mark-wiemer
Copy link
Member

RE removing unwanted stuff: there's always a way! Git is pretty good with ignoring entire files, as I'm sure you know. Just add something to the .gitignore. You can also look up "git update-index --assume-unchanged".

What things are you talking about, specifically? I think we can figure out a way to manage this better when I know the specifics. Please open a new discussion so we can discuss this further, I don't want to clutter the PR thread with a general discussion

@mark-wiemer
Copy link
Member

Hi @FuPeiJiang, would you mind making these changes? I would love to finally merge this

@mark-wiemer
Copy link
Member

@FuPeiJiang any plans to update this PR?

@mark-wiemer
Copy link
Member

Closing as stale, @FuPeiJiang you're welcome to re-investigate this issue and open another PR.

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

Successfully merging this pull request may close these issues.

2 participants