Skip to content

Commit

Permalink
Merge pull request #1 from eablokker/multiple-comb-selections
Browse files Browse the repository at this point in the history
Multiple comb selections
  • Loading branch information
eablokker committed Oct 16, 2017
2 parents 43268fd + dc19aa8 commit 3dcc250
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 17 deletions.
2 changes: 1 addition & 1 deletion CSScomb.espressoplugin/Contents/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<key>CFBundleIdentifier</key>
<string>com.austenblokker.CSScomb</string>
<key>CFBundleVersion</key>
<string>2.0.1</string>
<string>2.0.2</string>
<!--No need to modify these values-->
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
Expand Down
2 changes: 1 addition & 1 deletion CSScomb.espressoplugin/ScriptLibraries/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion CSScomb.espressoplugin/ScriptLibraries/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "CSScomb.espressoplugin",
"version": "2.0.1",
"version": "2.0.2",
"description": "CSScomb plugin for Espresso 3+",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
Expand Down
11 changes: 7 additions & 4 deletions CSScomb.espressoplugin/Scripts/csscomb.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,18 +182,21 @@ function processCSS(string) {
console.log(result);

}, function(err) {

errorDialog(err);
errorDialog(err.toString());

// Return selected CSS
console.log(err.css_);
console.log(string);
});
}

function escapeForShell(message) {
return message.replace(/\"/g, '\\"');
}

function errorDialog(err) {
var osascript = spawn(
'osascript',
['-e', 'tell application "Espresso" to display dialog "' + err + '" buttons "OK" default button 1 with title "CSScomb" with icon caution'],
['-e', "tell application \"Espresso\" to display dialog \"" + escapeForShell(err) + "\" buttons \"OK\" default button 1 with title \"CSScomb\" with icon caution"],
{
detached: true,
stdio: 'ignore'
Expand Down
39 changes: 31 additions & 8 deletions CSScomb.espressoplugin/Scripts/csscombAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,51 @@ action.performWithContext = function(context, outError) {

var scriptErrorMessage = 'CSScomb Node script failed';

var clearRecipe = new CETextRecipe();
// Comb entire file if no selection
var processFile = '';
if (selection.length === 0) {
var documentRange = new Range(0, context.string.length);

var clearRecipe = new CETextRecipe();
clearRecipe.deleteRange(documentRange);
// Stop script if this recipe fails
if (!context.applyTextRecipe(clearRecipe)) return false;

processFile = '-f ';
}

// Stop script if this recipe fails
if (!context.applyTextRecipe(clearRecipe)) return false;
var firstSnippet = new CETextSnippet('`if ! node -v > /dev/null; then echo "$EDITOR_SELECTION"; '+dialog(nodeMissingMessage)+'; elif ! test -d '+pluginsPath+'; then echo "$EDITOR_SELECTION"; '+dialog(scriptMissingMessage, 'caution')+'; else node '+scriptPath+' '+processFile+'-a "'+method+'" -t "'+tabString+'" -l "'+lineEndingString+'" || { '+dialog(scriptErrorMessage, 'caution')+'; echo "$EDITOR_SELECTION"; }; fi`');

var snippet = new CETextSnippet('`if ! node -v > /dev/null; then echo "$EDITOR_SELECTION"; '+dialog(nodeMissingMessage)+'; elif ! test -d '+pluginsPath+'; then echo "$EDITOR_SELECTION"; '+dialog(scriptMissingMessage, 'caution')+'; else node '+scriptPath+' '+processFile+'-a "'+method+'" -t "'+tabString+'" -l "'+lineEndingString+'" || { '+dialog(scriptErrorMessage, 'caution')+'; echo "$EDITOR_SELECTION"; }; fi`');
var snippet = new CETextSnippet('`node '+scriptPath+' '+processFile+'-a "'+method+'" -t "'+tabString+'" -l "'+lineEndingString+'" || { '+dialog(scriptErrorMessage, 'caution')+'; echo "$EDITOR_SELECTION"; }`');

function dialog(message, icon) {
return '{ nohup osascript -e \'tell application "Espresso" to display dialog "' + message + '" buttons "OK" default button 1 with title "CSScomb" with icon ' + ( icon ? icon : 'note' ) + '\' &> /dev/null& }';
}

// Stop script if this snippet fails
if (!context.insertTextSnippet(snippet, snippetOptions)) return false;

var newSelections = [];
var insertedOffset = 0;
var insertSnippets = selections.every(function(selection, index, array) {
var offsetLocation = selection.location + insertedOffset;
context.selectedRanges = [new Range(offsetLocation, selection.length)];

if (index === 0) {
if (!context.insertTextSnippet(firstSnippet, snippetOptions)) return false;
} else {
if (!context.insertTextSnippet(snippet, snippetOptions)) return false;
}

insertedOffset = context.selectedRanges[0].location - (selection.location + selection.length);
newSelections[index] = new Range(offsetLocation, context.selectedRanges[0].location - offsetLocation);
return true;
});

if (!insertSnippets) return false;

// Scroll back to where cursor was before
context.selectedRanges = selections;
if (selection.length === 0) {
context.selectedRanges = [selection];
} else {
context.selectedRanges = newSelections;
}
return true;
};
8 changes: 6 additions & 2 deletions CSScomb.espressoplugin/Scripts/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@ if (configpath !== null) {
} else {
// Error, no config file found:
message = 'No config file found.\n\n';
message += 'CSScomb first looks for a config file called .csscomb.json in your project\'s root folder, and then in parent folders up to your home folder.\n\n';
message += 'CSScomb first looks for a config file called ".csscomb.json" in your project\'s root folder, and then in parent folders up to your home folder.\n\n';
}

message += 'See csscomb.com for available config options.';

function escapeForShell(message) {
return message.replace(/\"/g, '\\"');
}

var osascript = spawn(
'osascript',
['-e', 'tell application "Espresso" to display dialog "' + message + '" buttons "OK" default button 1 with title "CSScomb" with icon note'],
['-e', "tell application \"Espresso\" to display dialog \"" + escapeForShell(message) + "\" buttons \"OK\" default button 1 with title \"CSScomb\" with icon note"],
{
detached: true,
stdio: 'ignore'
Expand Down

0 comments on commit 3dcc250

Please sign in to comment.