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

Else block always runs #150

Open
kylebarron opened this issue Sep 7, 2018 · 24 comments
Open

Else block always runs #150

kylebarron opened this issue Sep 7, 2018 · 24 comments

Comments

@kylebarron
Copy link
Owner

kylebarron commented Sep 7, 2018

@mcaceresb
This is because we run other commands after the if clause gets sent. Not really sure the best way to fix this. In Jupyter Notebook/Atom you can just be careful to only send the entire if/else block. In Jupyter console, you can fix this by wrapping the entire block in a pair of braces.

image
image

Console workaround:
image

@mcaceresb
Copy link
Collaborator

If there is more than one line of code, simply require two blank lines instead of one for the chunk to be considered complete.

@mcaceresb
Copy link
Collaborator

mcaceresb commented Sep 7, 2018

(i.e. two <CR> from the user)

@kylebarron
Copy link
Owner Author

Brilliant!

@kylebarron
Copy link
Owner Author

This is helpful for the console, but the behavior is still suboptimal in Hydrogen, since it doesn't use is_complete messages at all..
peek 2018-09-07 15-27

@kylebarron
Copy link
Owner Author

The only way to get around this in hydrogen right now is to manually select the if/else block. For now I should at least put this in the limitations page in the docs.

peek 2018-09-07 15-28

@mcaceresb
Copy link
Collaborator

Wait, how does it do it for python, then?

@kylebarron
Copy link
Owner Author

It doesn't. Since Hydrogen is 100% language-agnostic, it uses only general syntax elements like indentation and the foldEndMarker designated by the language's syntax highlighting package.

Recently a separate python-specific add-on package was developed to expand code lines. See nikitakit/hydrogen-python#10 for the difficulty of correctly implementing this feature.

This is something Hydrogen struggles with in general because of its language-independentness... Here's the same issue in Javascript.
peek 2018-09-07 15-47

I think it's something that could be improved in Hydrogen... If you had a suggestion I might mention it to the other maintainers and try to create a PR for it.

@mcaceresb
Copy link
Collaborator

and the foldEndMarker designated by the language's syntax highlighting package.

I thought you were in charge of that, or is it too much of a pain to implement? You can nest if/else in an ifelse fold if if is followed by else in the next line?

@kylebarron
Copy link
Owner Author

I am. And I edited it recently to include the ending } in Hydrogen.

But it's just a plain regex setting (that a core Atom package then implements) and Atom can't do regexes across lines anyways. So I don't think it's possible to say "} ends folds except when else { or else if { appears on the following line"

@kylebarron
Copy link
Owner Author

In Javascript, you could put }(?!\s* else) as the regex and plausibly get the whole if/else clause. But not sure how to do it when they're always on separate lines.

@mcaceresb
Copy link
Collaborator

Multi-line regex?

@kylebarron
Copy link
Owner Author

Atom can't do regexes across lines anyways

At least not for syntax highlighting. I doubt it allows it here, though I could try it out.

@mcaceresb
Copy link
Collaborator

mcaceresb commented Sep 7, 2018

Wait, this is odd. Python has multi-line strings. Doesn't it highlight them?

Conceptually, if should start an if-else environment ended by a blank line after } or an else block, right? I reckon that's not how it's written?

@kylebarron
Copy link
Owner Author

Wait, this is odd. Python has multi-line strings. Doesn't it highlight them?

Yes, but the token that it looks for in the regular expression never spans a newline. I.e. the """ will be on one line and another, and Atom can create regions between the lines, but the token can never span lines.

That's why it's impossible, for example, to highlight Setext headers in Markdown
burodepeper/language-markdown#192

@mcaceresb
Copy link
Collaborator

Actually, it is ended by an else block or a line containing anything that doesn't start with else

@mcaceresb
Copy link
Collaborator

But that is not the same, tho, right? Because you can look for environment delimiters...at least that is what I'm assuming.

@mcaceresb
Copy link
Collaborator

The markdown example has no delimiter to start the header, just to end it.

@kylebarron
Copy link
Owner Author

With the syntax highlighting yes. But not the fold end marker to my knowledge. I haven't tried but I'd be surprised.

@kylebarron
Copy link
Owner Author

kylebarron commented Sep 7, 2018

What's a simple regex to try?

}(?!\s*\n\s*else)

@mcaceresb
Copy link
Collaborator

Something like that, yeah. Remember that you can have if w/o brackets and else with brackets after afaik

@kylebarron
Copy link
Owner Author

It actually works when not using brackets:
image

I suppose the main reason is that when there are brackets I run the code with include

@kylebarron
Copy link
Owner Author

So include must break Stata's record of if/else somehow

@kylebarron
Copy link
Owner Author

The only case in which it doesn't work is when if uses brackets.

Why did we end up using include anyways?

peek 2018-09-07 17-10

@kylebarron
Copy link
Owner Author

Oh right we use include so that commands will stop running after one creates an error. Which is good...

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