Skip to content

Commit

Permalink
Merge pull request #2 from eablokker/comb-file-current-state
Browse files Browse the repository at this point in the history
Comb document current state instead of file
  • Loading branch information
eablokker authored Oct 22, 2017
2 parents 3dcc250 + e96d0c5 commit 174239a
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 60 deletions.
24 changes: 2 additions & 22 deletions CSScomb.espressoplugin/Scripts/csscomb.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
var yargs = require('../ScriptLibraries/node_modules/yargs');
var CSScomb = require('../ScriptLibraries/node_modules/csscomb');
var spawn = require('child_process').spawn;
var fs = require('fs');

var comb = new CSScomb();

Expand All @@ -13,12 +12,6 @@ var argv = yargs
describe: 'The string of CSS to comb',
type: 'string'
})
.option('processFile', {
alias: 'f',
describe: 'Process entire editorPath file',
default: 'false',
type: 'boolean'
})
.option('action', {
alias: 'a',
describe: 'Action to take',
Expand Down Expand Up @@ -159,20 +152,7 @@ function getSyntax() {
// Apply configuration:
comb.configure(getConfig());

if (argv.processFile) {
fs.readFile(editorPath, { encoding: 'utf8' }, function(err, data) {
if (err && err.path) {
errorDialog('Error: no such file or directory\n\n' + err.path);
return;
} else if (err) {
errorDialog(err);
return;
}
processCSS(data);
});
} else {
processCSS(selection);
}
processCSS(selection);

function processCSS(string) {
var combedCSS = comb.processString(string, { 'syntax': getSyntax() });
Expand All @@ -190,7 +170,7 @@ function processCSS(string) {
}

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

function errorDialog(err) {
Expand Down
68 changes: 33 additions & 35 deletions CSScomb.espressoplugin/Scripts/csscombAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,55 +14,53 @@ action.performWithContext = function(context, outError) {

var nodeMissingMessage = 'CSScomb was not able to find Node.js\n\nPlease install Node on your system and make sure the Node command is available in Terminal\n\nhttps://nodejs.org/en/download/';

var scriptMissingMessage = "CSScomb was not able to find your Plug-Ins folder\n\nCSScomb.espressoplugin must be installed in\n~/Library/Application Support/Espresso/Plug-Ins/";
var scriptMissingMessage = 'CSScomb was not able to find your Plug-Ins folder\n\nCSScomb.espressoplugin must be installed in\n~/Library/Application Support/Espresso/Plug-Ins/';

var scriptErrorMessage = 'CSScomb Node script failed';

// 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 ';
}

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 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+' -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"; }`');
var snippet = new CETextSnippet('`node '+scriptPath+' -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& }';
}

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;
});
function insertSnippets(selections) {

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

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

var insertedSnippetEnd = context.selectedRanges[0].location;

insertedOffset = insertedSnippetEnd - (sel.location + sel.length);
newSelections[index] = new Range(offsetLocation, insertedSnippetEnd - offsetLocation);
return true;
});

return insertSnippets;
}

if (!insertSnippets) return false;
// Comb entire file if no selection
if (selections.length <= 1 && selection.length === 0) {
var documentRange = new Range(0, context.string.length);
context.selectedRanges = [documentRange];

// Scroll back to where cursor was before
if (selection.length === 0) {
if (!insertSnippets(context.selectedRanges)) return false;
context.selectedRanges = [selection];
return true;
} else {
if (!insertSnippets(context.selectedRanges)) return false;
context.selectedRanges = newSelections;
return true;
}
return true;
};
2 changes: 1 addition & 1 deletion CSScomb.espressoplugin/Scripts/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ if (configpath !== null) {
message += 'See csscomb.com for available config options.';

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

var osascript = spawn(
Expand Down
2 changes: 1 addition & 1 deletion CSScomb.espressoplugin/Scripts/settingsAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ action.performWithContext = function(context, outError) {

var nodeMissingMessage = 'CSScomb was not able to find Node.js\n\nPlease install Node on your system and make sure the Node command is available in Terminal\n\nhttps://nodejs.org/en/download/';

var scriptMissingMessage = "CSScomb was not able to find your Plug-Ins folder\n\nCSScomb.espressoplugin must be installed in\n~/Library/Application Support/Espresso/Plug-Ins/";
var scriptMissingMessage = 'CSScomb was not able to find your Plug-Ins folder\n\nCSScomb.espressoplugin must be installed in\n~/Library/Application Support/Espresso/Plug-Ins/';

var scriptErrorMessage = 'CSScomb Node script failed';

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ This plugin is a fork of the original [CSScomb plugin for Espresso v2.1+](https:

## Requirements
- [Espresso v3+](http://macrabbit.com/espresso/)
- [Node.js (v6+)](http://nodejs.org/)
- [Node.js v6+](http://nodejs.org/)

Note: No longer requires ShellActions.sugar

Expand Down

0 comments on commit 174239a

Please sign in to comment.