Code Monkey home page Code Monkey logo

precursor's Introduction

Precursor for Brackets

A simple tool to speed up writing for loops, and similar syntax heavy code constructs, that can be described in a more concise way. It only takes a small amount of information to describe what you want, but for the JavaScript engine to understand you extra syntax is required.

What to expect

I've bound this to ctrl+shift+[
rewrites shorthand to longform

But this is a little like snippets, surely?

Precursor is to code what stenography is to reporting. Precursor and Snippets aim to achieve the same end goal, each using a different interaction paradigm.

How is this achieved?

Mostly through a cascade of Regex matching, if the content of a line doesn't match a first pattern it moves on to the next candidate, till a match is found. In this case the line can be rewritten from shorthand to longform. The current level of indentation is detected and used to make sure the rewrite happens where expected.

How to extend?

An example .json is provided with the extension, it includes the two main regexs that I use to avoid writing for-loops. It unfortunately requires you to escape backslashed regex commands. This means if you need a \S you need to write \\S, the same applies to any other command that starts with a \, they are listed here.

Regexs are tested in the order of appearance in prototyper.json, think of this as their order of precedence.

{
    "patterns": [
        {
            "pattern_name": "varname:iterations",
            "pattern_regex": "^(\\S+):(\\w+)$",
            "lines": [
                "for (var {0} = 0; {0} < {1}; {0} += 1) {",
                "{indent}{0};",
                "}"
            ]
        },
        {
            "pattern_name": "varname:iterable",
            "pattern_regex": "^(\\S+):(\\w+)\\[\\]?$",
            "lines": [
                "for (var {0} = 0; {0} < {1}.length; {0} += 1) {",
                "{indent}{1}[{0}];",
                "}"
            ]
        }
    ]
}

Testing

You should test if the addition you make to the prototyper.json results in a valid json file before attempting to run the extension. Use something like jsonlint.com if you aren't sure.

Possible issues

The regex will contain at least one match group ( ), but shouldn't use nested matches. Each set of ( ) will correspond to their numbered counterpart in the line property of the pattern. For example ^(\\S+):(\\w+)$ will match 2 parts of the input string, they can be accessed for the rewrite by using {0} and {1}.

In the case of no matches, there are a few potential reasons:

  • the regex should be moved up in the prototyper.json because likely some other regex is catching the content of the line before it reaches the intended regex.
  • the regex is not valid for the content of the line.
  • The regex is passed a trimmed version of the line you initiate the command on. Using ^ and $ at either end of your regex will help you declare an unambiguous pattern.

Comments?

Please use the issue tracker, I'll respond when time permits.

precursor's People

Contributors

zeffii avatar peey avatar claudiojs avatar

Stargazers

Dr. Manhattan avatar  avatar Imre Ardelean avatar  avatar Jidé avatar David Lane avatar

Watchers

James Cloos avatar

Forkers

peey brackets-io

precursor's Issues

Move license to another file

If the license was in another file, say /LICENSE.md rather than in the source, it'd be easier to see code license at a first visit to the project.

Organisation: make it installable via github url

In order to do that, the main.js file will have to be moved to the root directory of project.
Suggested: move the zip file to /build, move other 2 files in the zeffii.precursor directory to /src

writing shortform as iternotator.py used doesn't work so well in brackets

The scenario

i..some_value

i can be defined on the fly, the .. trailing it makes the next autosuggest run into troubles. As soon as one reaches i.. the brackets autosuggester will be considering a context (set of suggestions?) that won't include the current scope.

i..6  // works
i..some_value // works, but wont autosuggest 'some_value' at any point.
i..some_array[  // will suffer the same problem.

It might be necessary to consider a different shortform, or the whole plugin will be pointless.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.