Created
February 23, 2017 12:25
-
-
Save cybrox/35def92bf2b83d0d9b8375f79519c39d to your computer and use it in GitHub Desktop.
Rudimentary C format correcter written in Javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>C Formatter</title> | |
</head> | |
<body> | |
<textarea id="in" placeholder="IN" rows="10"></textarea> | |
<textarea id="out" placeholder="OUT" rows="10"></textarea> | |
<button id="go">GO</button> | |
<script type="text/javascript"> | |
const formInput = document.getElementById('in'); | |
const formOutput = document.getElementById('out'); | |
const formSubmit = document.getElementById('go'); | |
formSubmit.onclick = function() { | |
formOutput.value = format(formInput.value); | |
} | |
function format (input) { | |
const lines = input.split('\n'); | |
let analyzed = []; | |
let formatted = []; | |
let level = 0; | |
let active = true; | |
lines.forEach((line, index) => { | |
const squashedLine = line.trim().replace(/ +/g, ' '); | |
const startsComment = (line.indexOf('/*') !== -1); | |
const endsComment = (line.indexOf('*/') !== -1); | |
const hasComment = (line.indexOf('//') !== -1); | |
const startsLevel = (line.indexOf('{') !== -1); | |
const endsLevel = (line.indexOf('}') !== -1); | |
if (startsComment) { active = false; } | |
if (endsLevel) { level--; } | |
analyzed.push({ | |
active, | |
level, | |
comments: { | |
single: hasComment, | |
multi: (startsComment && endsComment) | |
}, | |
data: squashedLine | |
}); | |
if (startsLevel) { level++; } | |
if (endsComment) { active = true; } | |
}); | |
let output = ''; | |
analyzed.forEach((line, index) => { | |
let formattedLine = line.data; | |
// Remove tons of useless spaces | |
const padding = ' '.repeat(line.level); | |
formattedLine = `${padding}${formattedLine}`; | |
// Remove stupid next-line brackets | |
if (formattedLine.replace(/\/\*.*/, '').replace(/\/\/.*/, '').trim() === '{') { | |
let lastLine = formatted[(formatted.length - 1)]; | |
let minOffset = 1; | |
while (lastLine.replace(/\/\*.*/, '').replace(/\/\/.*/, '').trim().length === 0) { | |
lastLine = formatted[(formatted.length - (minOffset + 1))]; | |
minOffset += 1; | |
} | |
const parentLine = formatted[formatted.length - minOffset]; | |
if ((parentLine.indexOf('//') !== -1) || (parentLine.indexOf('/*') !== -1)) { | |
const offsetter = parentLine.match(/ +(\/\/|\/\*)/); | |
const parts = parentLine.split(/ +(\/\/|\/\*)/); | |
const joined = `${parts[0]} \{${offsetter}${parts[2]}`; | |
formatted[formatted.length - minOffset] = joined; | |
} else { | |
formatted[formatted.length - minOffset] += ' {'; | |
} | |
return; | |
} | |
// Right align stupid comments | |
if (line.comments.single || line.comments.multi) { | |
let foundStarter = -1 | |
let starterChar = '//'; | |
const singleStarter = formattedLine.indexOf('//'); | |
const multiStarter = formattedLine.indexOf('/*'); | |
if (singleStarter !== -1) { foundStarter = singleStarter; } | |
if (multiStarter !== -1) { foundStarter = multiStarter; } | |
if (foundStarter !== -1 && multiStarter !== -1) { starterChar = '/*' } | |
if (foundStarter !== -1) { | |
if (foundStarter < 100) { | |
const spacing = ' '.repeat(100 - foundStarter - (padding * 4)); | |
const parts = formattedLine.split(starterChar); | |
formattedLine = `${parts[0]}${spacing}${starterChar}${parts[1]}`; | |
} | |
} | |
} | |
formatted.push(formattedLine); | |
}); | |
output = formatted.join('\n'); | |
return output; | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment