Skip to content

Commit 26efd61

Browse files
author
Thierry Charbonnel
committed
replace match $1 $2...
1 parent 1551984 commit 26efd61

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

Find And Replace.sketchplugin/Contents/Sketch/FindAndReplace.sketchscript

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ var FindAndReplace = function (context) {
313313

314314
// Intelligent replace which attempts to match case if possible
315315
function doIntelligentReplace(replaceThis, replaceWith) {
316+
log(replaceThis + ' -- ' + replaceWith);
316317
var replacement = replaceWith;
317318
if (userDefaults.caseReplace == 1) {
318319
if (replaceThis.charAt(0) == replaceThis.charAt(0).toLowerCase()) {
@@ -331,6 +332,19 @@ var FindAndReplace = function (context) {
331332
return replacement;
332333
}
333334

335+
function replaceValue(sourceString, replaceString) {
336+
var newStringValue;
337+
if (!userDefaults.regex) {
338+
newStringValue = sourceString.replace(matchRegex,
339+
function(eachMatch){
340+
return doIntelligentReplace(eachMatch, replaceString);
341+
}
342+
);
343+
} else {
344+
newStringValue = sourceString.replace(matchRegex, replaceString);
345+
}
346+
return newStringValue
347+
}
334348

335349
// Do the actual search within the specified layer. Recursive to drill down into sublayers.
336350

@@ -344,7 +358,7 @@ var FindAndReplace = function (context) {
344358
case MSTextLayer:
345359
if ([layer stringValue].trim().match(matchRegex)) {
346360
itemsMatched++;
347-
layer.setStringValue(layer.stringValue().replace(matchRegex,function(eachMatch){return doIntelligentReplace(eachMatch, textToReplace);}));
361+
layer.setStringValue(replaceValue(layer.stringValue(), textToReplace));
348362
layer.setName(layer.stringValue().trim().replace(/(\r\n|\n|\r)/gm," "));
349363
}
350364
break;
@@ -389,11 +403,7 @@ var FindAndReplace = function (context) {
389403
overrides[index] = searchInSymbolsInception(overrides[index]);
390404
} else if(overrides[index].class().isSubclassOfClass_(NSString.class()) ) {
391405
if (overrides[index].trim() && overrides[index].trim().match(matchRegex)) {
392-
overrides[index] = overrides[index].replace(matchRegex,
393-
function(eachMatch){
394-
return doIntelligentReplace(eachMatch, textToReplace);
395-
}
396-
);
406+
overrides[index] = replaceValue(overrides[index], textToReplace);
397407
}
398408
}
399409
}
@@ -408,11 +418,7 @@ var FindAndReplace = function (context) {
408418
overrides[index] = searchInSymbolsInception(overrides[index]);
409419
} else if(overrides[index].class().isSubclassOfClass_(NSString.class()) ) {
410420
if (overrides[index].trim() && overrides[index].trim().match(matchRegex)) {
411-
overrides[index] = overrides[index].replace(matchRegex,
412-
function(eachMatch) {
413-
return doIntelligentReplace(eachMatch, textToReplace);
414-
}
415-
);
421+
overrides[index] = replaceValue(overrides[index], textToReplace);
416422
}
417423
}
418424
}

Find And Replace.sketchplugin/Contents/Sketch/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"author": "Martin Steven and Thierry Charbonnel",
55
"authorEmail" : "thierry.charbonnel@autreplanete.com",
66
"homepage": "https://github.com/thierryc/Sketch-Find-And-Replace/",
7-
"version": "1.9-beta",
7+
"version": "1.9-beta2",
88
"identifier": "cx.ap.sketch-find-and-replace",
99
"icon" : "runner-icon.png",
1010
"updateURL": "https://raw.githubusercontent.com/thierryc/Sketch-Find-And-Replace/master/Find%20And%20Replace.sketchplugin/Contents/Sketch/manifest.json",
749 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)