Skip to content
This repository has been archived by the owner on Sep 6, 2021. It is now read-only.

Adds code folding support for handlebars template files #12675

Merged
merged 10 commits into from
Oct 5, 2016

Conversation

thehogfather
Copy link
Contributor

Adds a new range finder to detect foldable regions and enable code folding for blocks and helpers in handlebars template files.

* @author Patrick Oladimeji
* @license MIT
* @date 14/08/2016 22:04:21
*/
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think you have to use the standard license comment of adobe on newer file now the code folding extension is in the core.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no problem - I'll remove it -artefact of automatic templates

@ficristo
Copy link
Collaborator

I don't know handlebar, so I left some general comments.
If nobody will look at the correctness of this, I'll give a try on some examples and eventually merge.


CodeMirror.registerHelper("fold", "handlebars", handlebarsFold);
CodeMirror.registerHelper("fold", "htmlhandlebars", handlebarsFold);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

will remove space.

@ficristo
Copy link
Collaborator

Another nit but in general Brackets use double quote for strings.

@@ -0,0 +1,174 @@
/**
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think you have to include the full licence comment here (like in other source files).
Beware the date has to be 2016 - present.

/*
 * Copyright (c) 2016 - present Adobe Systems Incorporated. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 *....
 *...

* @author Patrick Oladimeji
* @date 14/08/2016 22:04:21
*/

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ficristo BTW I notice this empty line is being introduced throughout the code base - although it conflicts with an existing recommendation in the fairly stale JS Docs guidelines. I am guessing that the guideline has simply not been updated?

Copy link
Collaborator

Choose a reason for hiding this comment

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

AFAIK the guidelines are still true. But until we can have stricter rules on our linter sometimes we miss things.

@zaggino zaggino added this to the Release 1.9 milestone Aug 28, 2016
CodeMirror.registerHelper("fold", "django", CodeMirror.helpers.fold.brace);
CodeMirror.registerHelper("fold", "tornado", CodeMirror.helpers.fold.brace);
CodeMirror.registerHelper("fold", "handlebars", handlebarsFold);
CodeMirror.registerHelper("fold", "htmlhandlebars", handlebarsFold);
Copy link
Collaborator

Choose a reason for hiding this comment

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

@thehogfather is the second parameter a CodeMirror mode? Where do you use it?
It seems to me that Mustache is not part of your PR, but handlebars is a Mustache like template.
And we use Mustache in the codebase.
So if it that param is a mode, I was wondering if we can use text/x-brackets-html or htmlmixed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yup that parameter is a CodeMirror mode. The name of the mode is used internally by CodeMirror to create a lookup table for the fold helper for a given mode.

define(function (require, exports, module) {
"use strict";
var CodeMirror = brackets.getModule("thirdparty/CodeMirror/lib/codemirror"),
endOfLineSpaceRegex = /\s$/,
Copy link
Collaborator

Choose a reason for hiding this comment

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

My little mind cannot understand this: are you looking for a single space, right? For which cases?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This looks for a space that occurs at the end of a string - and I have used it to scan a line until a space is reached. Very useful in finding potential opening range tags - e.g., {{ and {{foo in the following scenario:

{{ 
    foo 
    bar=baz 
    var-1=goo
}}

or

{{foo 
    bar=baz 
    var-1=goo
}}

And based on what is currently on the stack of the opening tags we try to search for a closing tag. I will add better comments to these. Perhaps endOfLineSpaceRegex is actually not a good name and naming the variable might be unnecessary in this scenario as it is only used in one place.

to: found.to
};
openTag = found.string.substring(0, found.string.length - 1);
if (openTag[0] === "#" || openTag[0] === "~") {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Here and below I think you have to add ^ and add a test for it and for ~

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. I was working with the handlebars page as reference. #* should also be added.

@ficristo
Copy link
Collaborator

I left a question and a request of change.
I didn't check everything, leaving that to the tests
Could merge master (again)? It will be easier for final check

@thehogfather
Copy link
Contributor Author

@ficristo PR has been updated.

@ficristo
Copy link
Collaborator

ficristo commented Oct 3, 2016

@thehogfather thank you.
I have some problems running all CodeFolding tests. Can you run them and are all green?

@ficristo
Copy link
Collaborator

ficristo commented Oct 3, 2016

On a PC where I was able to run tests I had a consistent failure on the can be enabled by setting makeSelectionsFoldable' to true` test.

@thehogfather
Copy link
Contributor Author

@ficristo - could you clarify if this use case also fails in use - and does it fail for all three file types, js, html, and handlebars? Could you change the preference in the project settings and see if selection fold is enabled in the editor.
All tests are passing on my mac - and I have no access to a PC to test that out.

@ficristo
Copy link
Collaborator

ficristo commented Oct 4, 2016

Actually I tested it on OSX too. I'll try to understand more the issue.

@thehogfather
Copy link
Contributor Author

And do you get the same failure on OSX?

@ficristo
Copy link
Collaborator

ficristo commented Oct 4, 2016

yes

@thehogfather
Copy link
Contributor Author

Very strange. Let me know if I can help - unfortunately, I have now tried different macs and I still cannot reproduce.

@ficristo
Copy link
Collaborator

ficristo commented Oct 4, 2016

I run again the tests on OSX and there still was a failure. I tryed to add a couple of breakpoints
and the test passed...
Then I tryed to enclose the selectTextInEditor call in a runs function and all the tests passed.

runs(function () {
  selectTextInEditor(start, end);
});

I can only guess there is some async problem, does it make sense to you?

@thehogfather
Copy link
Contributor Author

To be honest I"m a little baffled as to why that would fix it as the selectTextInEditor function itself uses the runs in its definition so the next block should not run until the selection is ready.

@ficristo ficristo merged commit 281a4df into adobe:master Oct 5, 2016
@ficristo
Copy link
Collaborator

ficristo commented Oct 5, 2016

I had some problem with code folding tests in the past too.
So, since they work for you, I decided to merge.
Thank you for you patience.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants