Giter Site home page Giter Site logo

Variables about snippets HOT 15 OPEN

atom avatar atom commented on July 30, 2024 13
Variables

from snippets.

Comments (15)

mark-hahn avatar mark-hahn commented on July 30, 2024 6

Would a PR be accepted that added these vars?

  • files
    • ${dirname} => /root/proj/lib
    • ${filename} => /root/proj/lib/main.coffee
    • ${basename} => main.coffee
    • ${extname} => .coffee
    • ${sep} => /
    • ${deliminator} => :
    • ${package} => /root/proj
    • ${dirnamerel} => lib
    • ${filenamerel} => lib/main.coffee
  • date & times (Note the colon)
    • ${:any-moment-format} current time in any format provided by the moment npm package
      • ${:MMMM Do YYYY, h:mm:ss a} => November 22nd 2014, 2:24:26 am
      • ${:dddd} => Saturday
      • ${:MMM Do YY} => Nov 22nd 14
  • positioning -- these variables always show an empty string
    • ${top} => insert entire snippet at top of file
    • ${bottom} => insert entire snippet at bottom of file
    • ${12} => insert such that top line of snippet is on line 12
    • ${cursor-3} => insert such that top line of snippet is 3 lines above cursor
  • Package.json (note the exclamation mark)

from snippets.

eleidan avatar eleidan commented on July 30, 2024 3

Another idea for variables I desperately need: ${clipboard}

Let's say I have a need to generate for my Rails project something as follows:

  # GET
  # /resource
  def index
  end

  # POST
  # /resource
  def create
  end

  # PATCH
  # /resource/:id
  def update
  end

  # DELETE
  # /resource/:id
  def destroy
  end

Let's say, I typed users, copied that text into clipboard, and
if I had an access to the clipboard with that very text, I would get ready-to-use code blocks like follows:

  # DELETE
  # /users/:id
  def destroy
  end

and for copied teams:

  # DELETE
  # /teams/:id
  def destroy
  end

With the following snippet:

  'DELETE':
    'prefix': 'delete'
    'body': """
    # DELETE
    # /${clipboard}/:id
    def destroy
    end
    """

from snippets.

73 avatar 73 commented on July 30, 2024 2

Just to add another idea (I admit, one that I desperately need and did not find) would be the selected text [1]. I would love to mark something, press a hotkey and encapsulate the selected text.
In Gedit it is possible with something like that: \emph{${1:$GEDIT_SELECTED_TEXT}}$0 for LaTeX.

May this be done with this plugin?

[1] https://wiki.gnome.org/Apps/Gedit/Plugins/Snippets

from snippets.

savetheclocktower avatar savetheclocktower commented on July 30, 2024 2

My guess is that this never got prioritized because there are ways of achieving the same effect via commands.

For instance, if you're trying to replicate TextMate's $TM_SELECTED_TEXT variable, you could do something like this in your init file:

// WRAP IN HTML TAG
atom.commands.add('atom-text-editor', {
  'custom:wrap-in-html-tag': () => {
    let editor = atom.workspace.getActiveTextEditor();
    let selection = editor.getLastSelection();
    let snippet = atom.packages.activePackages.snippets.mainModule;
    snippet.insert('<${1:div}>' + selection.getText() + '</${1:div}>');
  }
});

So this feels like one of those features where anyone who knows Atom well enough to write the PR also knows alternative strategies for these use cases. But that's not an argument against the feature β€” just a description of how we got here.

I'd happily accept a PR. Looks like VS Code has implemented snippet variables also, so I'd be cool with a PR that added all (or a meaningful subset of) the variables on their list. Might as well standardize.

If anyone wants to take a stab at this, I can probably help them through some of the tricky parts, like changing the parser grammar.

from snippets.

mark-hahn avatar mark-hahn commented on July 30, 2024

See #83

Edit: @Brandon-Grimshaw, you wouldn't want to help test this, would you? (grin)

from snippets.

dwelle avatar dwelle commented on July 30, 2024

πŸ‘ for the ${clipboard} variable.

from snippets.

raptor235 avatar raptor235 commented on July 30, 2024

+1 for clipboard as well

some basic features that sublime has taken care of over the years but are essential to proper macro building

from snippets.

Chukatuk avatar Chukatuk commented on July 30, 2024

+1 clipboard xD

from snippets.

sixertoy avatar sixertoy commented on July 30, 2024

Clipboard!!!

from snippets.

pgiani avatar pgiani commented on July 30, 2024

+1 for clipboard as well

from snippets.

dublx avatar dublx commented on July 30, 2024

+1 for clipboard

from snippets.

simonc avatar simonc commented on July 30, 2024

The lack of variables like this is one of the biggest missing features that prevent me from switching from TM2 to Atom.

As a maker of snippets that use this type of variables, I would advise the addition of some transformation capabilities. For instance a snippet I use in TM2 looks like this

module Processes
  module ${TM_DIRECTORY/(?:\A|.+\/|_)([A-Za-z0-9]+)(?:\.[a-z]+)*/(?2::\u$1)/g}
    class ${TM_FILENAME/(?:\A|_)([A-Za-z0-9]+)(?:\.[a-z]+)*/(?2::\u$1)/g}
      $0
    end
  end
end

Sure, that's not pretty at all, but with a file named client_accounts/list_all.rb it would make

module Processes
  module ClientAccounts
    class ListAll

    end
  end
end    

You can't imagine the time I save with this kind of nuggets but the ability to transform the folder and filename values is key.

Cheers 😊

from snippets.

tjpeden avatar tjpeden commented on July 30, 2024

What happened to this?

from snippets.

wasimxe avatar wasimxe commented on July 30, 2024

// WRAP IN HTML TAG atom.commands.add('atom-text-editor', { 'custom:wrap-in-html-tag': () => { let editor = atom.workspace.getActiveTextEditor(); let selection = editor.getLastSelection(); let snippet = atom.packages.activePackages.snippets.mainModule; snippet.insert('<${1:div}>' + selection.getText() + '</${1:div}>'); } });

I tried pasting it in init file and error appear "Reserved word let" What should be issue?

from snippets.

savetheclocktower avatar savetheclocktower commented on July 30, 2024

I tried pasting it in init file and error appear "Reserved word let" What should be issue?

I don’t know; I pasted that code snippet into my init.js just now and it worked fine. Try the forums or the Slack channel for tech support.

from snippets.

Related Issues (20)

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.