How to select JSON grammar from atom command script? - atom-editor

I'm using Atom with my init.js below, the objective are :
select all
Prettify json
Change grammar to json
I'm having difficulties with step #3, below is my script :
atom.commands.add("atom-workspace", {
"custom:prettify-json": function() {
var view;
view = atom.views.getView(atom.workspace.getActiveTextEditor());
atom.commands.dispatch(view, "core:select-all");
atom.commands.dispatch(view, "pretty-json:prettify");
atom.commands.dispatch(view, "grammar-selector:show");
return true;
}
});
So far I only found that grammar-sellector only has show function, is there any way to choose "source.json" ?

There's the private method setGrammar() that has been around since forever. It should serve the task, unless the developers decide to remove/replace it in future versions.
atom.commands.add("atom-workspace", {
"custom:prettify-json": function() {
const editor = atom.workspace.getActiveTextEditor();
const view = atom.views.getView(editor);
atom.commands.dispatch(view, "core:select-all");
atom.commands.dispatch(view, "pretty-json:prettify");
editor.setGrammar(atom.grammars.grammarForScopeName('source.json'));
return true;
}
});

Related

Can we use Web API such as new CSSStyleSheet() while vs code extension development?

Can we use web API while development of a vs code extension ?
I was trying to do something like below
let activeEditor = vscode.window.activeTextEditor;
function replaceWithinDocument() {
if (!activeEditor) {
return;
}
const { document } = activeEditor;
const text = document.getText();
//console.log({ text })
const stylesheet = new CSSStyleSheet();
// Add some CSS
stylesheet.replaceSync(text);
}
but it throw error of because of new CSSStyleSheet()
so can't we use or is there any other way to write it ?

How to write Meteor.wrapAsync fs.readFile?

I need a function that emits individual lines from a file with newlines. Nothing hard.
But with node, it is hard, and with Meteor, there's an additional complication: you must use Meteor.wrapAsync. Surprisingly, there isn't an example of how to use wrapAsync in the docs, and I could only find a couple of examples online, none of which helped.
I have something like:
var readFileAsync = function (file, cb) {
// From here to below comment works synchronously
var instream = fs.createReadStream(file, function () {
var outstream = new stream;
outstream.readable = true;
outstream.writable = true;
var rl = readline.createInterface({
input: instream,
output: outstream,
terminal: false
});
rl.on('line', function(line) {
console.log(line);
return line;
});
});
// Reference to aforementioned comment
};
var readWatFile = Meteor.wrapAsync(readFileAsync);
var line = readWatFile('/path/to/my/file');
console.log(line);
I know this is wrong because it doesn't work, so how do I write this?
There are two ways to go around it.
Load the whole file into memory and do whatever you want. To do that you can use the Private Assets API
Use node.js streams and stream the file line by line. You would have something like this.
Example code that you would need to tweak to your favorite streaming methods:
var Future = Npm.require('fibers/future');
var byline = Npm.require('byline');
var f = new Future;
// create stream in whatever way you like
var instream = fs.createReadStream(...);
var stream = byline.createStream(instream);
// run stream handling line-by-line events asynchronously
stream.on('data', Meteor.bindEnvironment(function (line) {
if (line) console.log(line)
else future.return();
}));
// await on the future yielding to the other fibers and the line-by-line handling
future.wait();

Is there a way to attach callback what fires whenever a crossfilter dimension filter changes?

I have several charts built with dc.js. I can achieve the desired functionality by attaching a callback to each dc.js chart's .on("filterted", function(chart) {}) but this is annoying because I have to attach the same callback to each chart. And error prone because as new charts are added, someone has to remember to attach an event hander. I would prefer to just attach a callback to the underlying crossfilter. Is that possible?
Is there a way to optimize this...
var ndx = crossfilter(data);
var dimAlpha = ndx.dimension(function(d) {return d.alpha});
var dimBeta = ndx.dimension(function(d) {return d.beta});
var groupAlpha = dimAlpha.group().reduceSum(function(d) {return 1;});
var groupBeta = dimBeta.group().reduceSum(function(d) {return 1;});
dc.pieChart(myDomId1)
.dimension(dimAlpha)
.group(groupAlpha)
.on("filtered", function(chart) {
//do stuff
});
dc.pieChart(myDomId2)
.dimension(dimBeta)
.group(groupBeta)
.on("filtered", function(chart) {
//do stuff
});
into something like this...
var ndx = crossfilter(data);
var dimAlpha = ndx.dimension(function(d) {return d.alpha});
var dimBeta = ndx.dimension(function(d) {return d.beta});
var groupAlpha = dimAlpha.group().reduceSum(function(d) {return 1;});
var groupBeta = dimBeta.group().reduceSum(function(d) {return 1;});
dc.pieChart(myDomId1)
.dimension(dimAlpha)
.group(groupAlpha);
dc.pieChart(myDomId2)
.dimension(dimBeta)
.group(groupBeta);
ndx.on("filtered", function() {
//do stuff
})
If you've got a million charts and don't want to have to attach the event listener to each one manually, you could iterate through the chart registry and add them that way. Ex:
dc.chartRegistry.list().forEach(function(chart) {
chart.on('filtered', function() {
// your event listener code goes here.
});
});
Note that this code must go after the charts have instantiated to work.
In the absence of a way to attach the callback once globally, one thing you could do to mitigate the risk from duplicate code is to define the callback function once and pass in a reference instead of defining it inline on each chart.
function my_func() {
// do stuff
}
dc.pieChart(myDomId2)
.dimension(dimBeta)
.group(groupBeta)
.on("filtered", my_func);
chart and filter can also be passed to the filter function something like:
function my_func(chart,filter) {
// do stuff
}
dc.pieChart(myDomId2)
.dimension(dimBeta)
.group(groupBeta)
.on("filtered", my_func);

XPages Dojo DataStore Get and Cycle Through Return Values

My goal is to grab all the rows in the DataGrid that I display, cycle through each row, grab certain items and values, and update fields in the UI. Based on what I have read, it is best to grab the datastore and go through that.
The user is editing the Dojo datagrid (editable columns) and clicking a Save button to save the changes (RESTService.save()). During the Save button process, I would like to cycle through the datastore.
I am using examples from this:
http://xcellerant.net/2013/06/13/dojo-data-grid-21-locking-columns/
This is my REST Service:
<xp:this.resources>
<xp:dojoModule name="dojo.data.ItemFileWriteStore"></xp:dojoModule></xp:this.resources><xe:restService id="rsVictims" pathInfo="rsVictimsData">
<xe:this.service>
<xe:viewJsonService defaultColumns="true"
viewName="InvoiceMPRVictims">
<xe:this.databaseName><![CDATA[#{javascript:applicationScope.get("appConfig").keywords.appDataStore.join("!!");}]]></xe:this.databaseName>
<xe:this.keys><![CDATA[#{javascript:viewScope.get("mprKeysValue");}]]></xe:this.keys>
</xe:viewJsonService>
</xe:this.service>
</xe:restService>
This is the Save button:
<xp:button value="Save Changes" id="victimsSaveButton">
<xp:eventHandler event="onclick" submit="false">
<xp:this.script><![CDATA[// Save the changes...
try{
rsVictims.save();
var jsonStore;
// Get the datastore...
jsonStore = new dojo.data.ItemFileWriteStore({url: "invoices_page1_doc.xsp/rsVictimsData"});
// Cycle through the jsonStore...
}
catch (e) {
alert(e);
}
]]></xp:this.script>
</xp:eventHandler>
</xp:button>
I was hoping to get a few examples on how to cycle through the datastore return. I see ideas in this reference, but not sure which one to use:
http://dojotoolkit.org/reference-guide/1.9/dojo/data/ItemFileWriteStore.html
Also, does saving the REST Service (rsVictims.save();) before grabbing the datastore give me the current values?
Thanks!
-------------- EDIT 1/6/2014 ----------------------
I have updated the Save button to use this code:
try {
rsVictims.save();
var item;
var itemStore;
var itemName;
var itemValue;
// Get the datastore...
itemStore = new dojo.data.ItemFileReadStore( {
url : "invoices_page1_doc.xsp/rsVictimsData"
});
itemName = "month_11";
function failed(error) {
// Do something with the provided error.
alert(error);
}
function gotItem(item) {
if (itemStore.isItem(item)) {
itemValue = itemStore.getValue(item, itemName);
alert(itemValue);
} else {
// This should never occur.
throw new Error("Unable to locate the item with identity [sv]");
}
}
// Invoke the lookup. This is an async call as it may have to call back to a
// server to get data.
itemStore.fetchItemByIdentity( {
identity : itemName,
onItem : gotItem,
onError : failed
});
} catch (e) {
alert(e);
}
However, I do not get any return values in the gotItem function and I receive no errors.
I may have missed something -- any help would be great!
Thanks!

Is it possible to read from console in Dart?

I know that we can print to the console in dart using the print() statement.
I want to know if it is possible to read data from console. I did a search and also looked in the dart:io package, but couldn't find any reference.
Thanks
You can use StringInputStream to read from stdin like this
#import("dart:io");
main() {
var stream = new StringInputStream(stdin);
stream.onLine = () {
var line = stream.readLine();
if (line != null) {
print(line);
}
};
}
also if you're developing a console application then checkout the Options class to parse command line arguments
final args = new Options().arguments;

Resources