Skip to content

Commit

Permalink
Fix include sorting, overwriting, and invalid chars
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxattax97 committed Mar 15, 2019
1 parent 54c580b commit 1f0e6e8
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
BasedOnStyle: Mozilla
ColumnLimit: 80
SortIncludes: true
# SortIncludes: true
IndentWidth: 4
AccessModifierOffset: -4
ContinuationIndentWidth: 4
Expand Down
40 changes: 23 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ const { argv } = require('yargs')
.alias('f', 'force')
.nargs('f', 1)
.describe('f', 'Forcibly overwrite the source file')
.alias('c', 'config')
.nargs('c', 1)
.describe('c', 'Use the specified config using the .clang-format style file')
.alias('j', 'javadoc')
.nargs('j', 1)
.describe('j', 'Automatically add {Java,JS}doc-style comment templates to functions and modules where missing')
.alias('d', 'dry')
.nargs('d', 1)
.describe('d', 'Perform a dry run, without writing')
Expand All @@ -34,11 +40,12 @@ tmp.setGracefulCleanup();

async function convertIncludesToClang(str) {
// eslint-disable-next-line no-useless-escape
const regex = /^\s*(include|use)\s*<([\.\w\/]*)>\s*$/gm;
const regex = /^\s*(include|use)\s*<([-\.\w\/]*)>\s*$/gm;

// {type: 'include' | 'use', path: 'cornucopia/../source.scad'}
const backup = [];
let matches = regex.exec(str);
let updated = str;

while (matches !== null) {
if (matches.index === regex.lastIndex) {
Expand All @@ -50,33 +57,25 @@ async function convertIncludesToClang(str) {
matches.forEach((match, groupIndex) => {
if (groupIndex === 0) {
entry = {};
entry.full = match;
} else if (groupIndex === 1) {
entry.type = match;
} else if (groupIndex === 2) {
entry.path = match;
updated = updated.replace(entry.full.trim(), `#include <${entry.path}>`);
backup.push(entry);
}
});

matches = regex.exec(str);
}

const updated = str.replace(regex, '');

const newIncludes = [];
backup.forEach((item) => {
// Use include since we're converting to Clang.
newIncludes.push(`#include <${item.path}>\n`);
});
newIncludes.push('\n'); // Add a newline to separate the includes from source.
newIncludes.push(updated);

return { str: newIncludes.join(''), backup };
return { str: updated, backup };
}

async function convertIncludesToScad(str, backup) {
// eslint-disable-next-line no-useless-escape
const regex = /^\s*#include\s*<([\.\w\/]*)>\s*$/gmi;
const regex = /^\s*#include\s*<([-\.\w\/]*)>\s*$/gmi;
let fixed = str;
let matches = regex.exec(str);

Expand All @@ -92,11 +91,18 @@ async function convertIncludesToScad(str, backup) {
entry = { full: match };
} else if (groupIndex === 1) {
entry.path = match;
backup.forEach((item) => {
if (item.path === entry.path) {
fixed = fixed.replace(entry.full, `${item.type} <${item.path}>`);

// Must traverse in order.
for (let i = 0; i < backup.length; i += 1) {
if (backup[i].path === entry.path) {
// Replace only _a single occurance_.
fixed = fixed.replace(new RegExp(entry.full.trim(), ''), `${backup[i].type} <${backup[i].path}>`, '');

// Splice out the one we just performed.
backup.splice(i, 1);
break;
}
});
}
}
});

Expand Down
11 changes: 8 additions & 3 deletions test/comparing/source.scad
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
include <cornucopia/util/constants.scad>
include <cornucopia/util/math.scad>
include <cornucopia/util/measures/imperial.scad>
use <cornucopia/util/vector.scad>

include <cornucopia/util/constants.scad>

use <cornucopia/util/constants.scad>

// This file is placed under the public domain

// from: http://www.thingiverse.com/thing:9512
Expand Down Expand Up @@ -193,9 +198,9 @@ module block(width,
translate([
xcount * knob_spacing,
ycount * knob_spacing,
-roof_thickness / 2
-,roof_thickness / 2
]) cylinder(r = knob_diameter / 2,
, h = height * block_height + roof_thickness,
h = height * block_height + roof_thickness,
$fs = cylinder_precision);
}
// posts:
Expand Down
4 changes: 3 additions & 1 deletion test/dirty/source.scad
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
include <cornucopia/util/math.scad>
include <cornucopia/util/measures/imperial.scad>

use <cornucopia/util/vector.scad>

include <cornucopia/util/constants.scad>

use <cornucopia/util/constants.scad>


// This file is placed under the public domain

// from: http://www.thingiverse.com/thing:9512
Expand Down

0 comments on commit 1f0e6e8

Please sign in to comment.