Automatic css closing bracket in Sublime Text 3 - css

When creating a new CSS rule, I would like the closing bracket to be placed automatically right under the class name, so as a result, later, it would stay on the same line as the last property: value pair:
.rule {
display: block;
clor: #000; }
.rule {
} /* It should look like this when pressing enter after
the opening bracket with the cursor before the closing bracket (obviously). */
I have tried “auto_match_enabled”: false but this simply disables the closing bracket, so I need to add it manually.
I have installed PackageResourceViewer and looked into CSS.sublime-syntax and css_completions.py but could not find any corresponding setting or code. I have also found nothing in the default snippets.
EDIT
How could I get below result?
.rule {
| }

To determine what's happening in response to a key press, open the Sublime console with View > Show Console or the associated key, and enter sublime.log_commands(True) to turn on command logging, then take the action.
In doing this, you can see that in a situation like this:
.rule {|}
When you press Enter, the command that triggers is:
command: run_macro_file {"file": "res://Packages/Default/Add Line in Braces.sublime-macro"}
From this you can determine that the key is bound to run_macro_file, and that the macro is what's taking this action. If you look in the default key bindings, the key binding is:
{ "keys": ["enter"], "command": "run_macro_file", "args": {"file": "res://Packages/Default/Add Line in Braces.sublime-macro"}, "context":
[
{ "key": "setting.auto_indent", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "\\{$", "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^\\}", "match_all": true }
]
},
That is, if you press Enter while auto_indent is turned on, the selection is empty, and the cursor is sitting in the middle of two braces {}, run the macro, which takes the step of inserting multiple lines.
Simplistically, you can thus stop this from happening by turning off auto_indent. However that's not a viable solution generally speaking, so you would need to create a key binding in your User package that, in the same situation doesn't do this and instead just does what enter would do:
{ "keys": ["enter"], "command": "insert", "args": {"characters": "\n"}, "context":
[
{ "key": "selector", "operator": "equal", "operand": "source.css" },
{ "key": "setting.auto_indent", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "\\{$", "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^\\}", "match_all": true }
]
},
This is the same binding as above, but now the command just inserts a single line, and it also constrains itself only to CSS files via the source.css match in the selector.
With this in place, the css looks like the following when you press the key in this situation:
.rule{
|}
EDIT
The Text in the characters argument of the insert command can be set to any text that you'd like to type, so you could for example set it to "\n\t" or "\n " (newline and two spaces) in order to have a bit of indentation on the new line.
In order to achieve a result like the following, a slightly different command is required:
.rule {
| }
Here there is space before and after the cursor, which precludes the use of the insert command since the cursor always ends up after the last character inserted.
One way to to do this would be to create a macro similar to what already happens for the default binding of this key, but another way would be the insert_snippet command. This can insert a snippet file but it also takes an argument of contents that tells it the snippet content directly.
With that in mind, the above key binding could be expressed as:
{ "keys": ["enter"], "command": "insert_snippet", "args": {"contents": "\n\t$0\t"}, "context":
[
{ "key": "selector", "operator": "equal", "operand": "source.css" },
{ "key": "setting.auto_indent", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "\\{$", "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^\\}", "match_all": true }
]
},
insert_snippet expands out the snippet text (including any fields, if any are provided) and then places the cursor at $0, which defaults to the end of the inserted content if not provided.
Inserting a \t character inserts a tab, which will be a physical tab character unless translate_tabs_to_spaces is turned on, in which case the insertion will replace the \t with the same number of spaces as tab_size is currently set to.
You could of course also use a specific amount of space characters instead, if that is desired.

Related

Storing optional attributes in DynamoDB's putItem via step functions

I have defined a state machine in AWS step functions and one of my states is storing an item to DynamoDB
...
"Store item": {
"End": true,
"Type": "Task",
"Resource": "arn:aws:states:::dynamodb:putItem",
"Parameters": {
"Item": {
"foo": {
"S.$": "$.data.foo"
},
"bar": {
"S.$": "$.data.bar"
},
"baz": {
"S.$": "$.data.baz"
},
},
"TableName": "nrp_items"
}
},
...
The problem starts from the fact that baz property is optional, ie not exist in some cases.
On those cases, the putItem task fails:
An error occurred while executing the state 'Store item' (entered at the event id #71). > The JSONPath '$.data.baz' specified for the field 'S.$' could not be found in the input
My backup plan is to use a lambda to perform that type of operation, but can I do it directly using the putItem task in steps function?
I was wondering if:
Is possible to somehow inject via JSONPath my whole $.data item to the "Item" property, something like:
...
"Store item": {
"End": true,
"Type": "Task",
"Resource": "arn:aws:states:::dynamodb:putItem",
"Parameters": {
"Item": "$.data",
"TableName": "nrp_items"
}
},
...
OR
2) Define that the baz property is optional
TL;DR We can deal with optional variables with a "Variable": "$.baz", "IsPresent": true Choice condition to handle no-baz cases.
The Amazon States Language spec does not have optional properties: Step Functions will throw an error if $.baz does not exist in the input. We can avoid undefined paths by inserting a two-branch Choice State, one branch of which handles baz-exists cases, the other no-baz cases. Each branch continues with a Pass State that reworks the data input into dynamo-format Item syntax, using Parameters. The put-item task's "Item.$": "$.data" (as in your #1) contains only foo-bar when baz is not defined, but all three otherwise.
{
"StartAt": "HasBazChoice",
"States": {
"HasBazChoice": {
"Type": "Choice",
"Choices": [
{
"Variable": "$.baz",
"IsPresent": true,
"Next": "MakeHasBazItem"
}
],
"Default": "MakeNoBazItem"
},
"MakeHasBazItem": {
"Type": "Pass",
"Parameters": {
"data": {
"foo": { "S.$": "$.foo"},
"bar": { "S.$": "$.bar"},
"baz": { "S.$": "$.baz"}
}
},
"Next": "PutItemTask"
},
"MakeNoBazItem": {
"Type": "Pass",
"Parameters": {
"data": {
"foo": {"S.$": "$.foo"},
"bar": {"S.$": "$.bar"}
}
},
"Next": "PutItemTask"
},
"PutItemTask": {
...
"Parameters": {
"TableName": "my-table",
"Item.$": "$.data"
}
},
}
}
If you have more than one optional field, your lambda backup plan is the better option - the above workaround would become unwieldy.

how to get specific items data from google calendar api

hi I am using google api to get all the events which is working fine. There is a parameter where I can specify fields name which I need to retrieve. There is a filed named as items where all the events data are coming. Now I want to get specific data of that items filed. Not all the data.
This is how I am getting all the data of that items.
https://www.googleapis.com/calendar/v3/calendars/CALENDAR_ID/events?showDeleted=true&timeMax=2021-08-16T06%3A00%3A46.000Z&timeMin=2021-04-16T06%3A00%3A46.000Z&prettyPrint=true&fields=items&key=[YOUR_API_KEY]
This is how data are coming.
{
"items": [
{
"kind": "calendar#event",
"etag": "\"hfsfjadj\"",
"id": "adfjadfjad",
"status": "confirmed",
"htmlLink": "https://www.google.com/calendar/event?eid=adfjdafjd",
"created": "2021-05-24T05:47:42.000Z",
"updated": "2021-05-24T05:47:42.331Z",
"summary": "dafhafj",
"creator": {
"email": "adfjadfj#gmail.com",
"self": true
},
"organizer": {
"email": "adfjadfj#gmail.com",
"self": true
},
"start": {
"dateTime": "2021-05-24T11:30:00+06:00"
},
"end": {
"dateTime": "2021-05-24T12:30:00+06:00"
},
"iCalUID": "dfjdfjdfj#google.com",
"sequence": 0,
"reminders": {
"useDefault": true
},
"eventType": "default"
}
]
}
I wanted to fetch only id, start,end and summary not the entire thing which is given above.
You can specify nested fields by enclosing them in round brackets, and multiple fields should be separated by commas. Your provided fields would be something like this:
items(id,start,end,summary)
Therefore, the URL would change from this:
https://www.googleapis.com/calendar/v3/calendars/CALENDAR_ID/events?showDeleted=true&timeMax=2021-08-16T06%3A00%3A46.000Z&timeMin=2021-04-16T06%3A00%3A46.000Z&prettyPrint=true&fields=items&key=[YOUR_API_KEY]
To this:
https://www.googleapis.com/calendar/v3/calendars/CALENDAR_ID/events?showDeleted=true&timeMax=2021-08-16T06%3A00%3A46.000Z&timeMin=2021-04-16T06%3A00%3A46.000Z&prettyPrint=true&fields=items(id%2Cstart%2Cend%2Csummary)&key=[YOUR_API_KEY]
Reference:
Partial response

Specify the starting cell of a Jupyter notebook

Is there a convenient way to specify the starting cell of a Jupyter notebook?
I'm thinking of something along the lines of the HTML input autofocus Attribute.
For example,
{
"cells": [{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Run your first Bash command"
]
}, {
"autofocus": true,
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"echo \"Hello World\""
]
}]
}
The The Notebook file format doesn't have anything like this but maybe there's another solution out there.

Change Sublime Text Bracket/Indent Rules

I'm trying to figure out how to change Sublime Texts auto bracket rules for css.
I get this by default:
.class {
#CURSOR
}
I would like to have this:
.class {
#CURSOR}
Any ideas on how to accomplish this?
You can add this shortcut to your Key Bindings - User:
{ "keys": ["enter"], "command": "insert", "args": {"characters": "\n\t"}, "context":
[
{ "key": "setting.auto_indent", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "\\{$", "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^\\}", "match_all": true },
{ "key": "selector", "operator": "equal", "operand": "source.css" }
]
}
to modify enter key behaviour with css files.
Or you can use a Snippet. Tools/New Snippet...:
<snippet>
<content><![CDATA[
{
${1}}
]]>
</content>
</snippet>
Save it as Packages/User/CSSBrackets.sublime-snippet.
Then, add a shortcut in your Key Bindings - User to trigger it when pressing { in css files:
{ "keys": ["{"], "command": "insert_snippet", "args": {"name": "Packages/User/CSSBrackets.sublime-snippet"},
"context":
[
{ "key": "selector", "operator": "equal", "operand": "source.css" }
]
}

How to use another brackets/tabs policy for my CSS?

Here is an example of what is happening now:
.class {
}
So when I'm typing .class { SublimeText automatically inserts } (which is correct).
Then I press Enter and get this:
.class {
#CURSOR_POSITION#
}
But what I really want is (attention to the closing bracket):
.class {
#CURSOR_POSITION#
}
I've even seen (ok, it was only once) in some editor special setting for this. Now I start using SublimeText (which is cool!) and I feel that it can be customized in a such way but I'm not quite sure how.
Ok! It's not really hard to do it but it was not simple to know how to do it =)
So my approach is:
Write a simple macro (I've edited the default Enter macro called Add Line in Braces.sublime-macro which lives in ~Data/Packages/Default) and save it with a new name.
I've called it CSS.Add Line in Braces.sublime-macro and put in ~Data/Packages/User.
[
{"command": "insert", "args": {"characters": "\n\n"} },
{"command": "move_to", "args": {"to": "hardbol", "extend": false} },
{"command": "insert", "args": {"characters": "\t"} },
{"command": "move", "args": {"by": "lines", "forward": false} },
{"command": "move_to", "args": {"to": "hardeol", "extend": false} },
{"command": "reindent", "args": {"single_line": true} }
]
Apply it for Enter key in CSS files. For that we need to fo to Preferences > Key Bindings - User (it will open ~Data/Packages/User/Default (YOUR_OPERATING_SYSTEM).sublime-keymap) and paste there the following code:
[
{ "keys": ["enter"], "command": "run_macro_file", "args": {"file": "Packages/User/CSS.Add Line in Braces.sublime-macro"}, "context":
[
{ "key": "selector", "operator": "equal", "operand": "source.css" },
{ "key": "setting.auto_indent", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
{ "key": "preceding_text", "operator": "regex_contains", "operand": "\\{$", "match_all": true },
{ "key": "following_text", "operator": "regex_contains", "operand": "^\\}", "match_all": true }
]
}
]
It is copy-pasted from the defult keybinding file with one context addition:
{ "key": "selector", "operator": "equal", "operand": "source.css" }
which tells Sublime to apply it only for CSS ext.
Profit!

Resources