Skip to content

Commit

Permalink
Added ability to format partial content (cont')
Browse files Browse the repository at this point in the history
  • Loading branch information
Anantachai Saothong (Manta) committed Apr 15, 2017
1 parent 1ec74bd commit bfb5f51
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 4 deletions.
9 changes: 5 additions & 4 deletions edge/format.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ function format(content, options) {
// This is designed for https://github.com/ThisIsManta/vscode-stylus-supremacy
if (originalLines.length === 1) {
modifiedContent = 'wrap\n\t' + content.trim()
originalBaseIndent = _.get(content.match(/^(\s|\t)*/g), '0', null)

} else {
// Determine an original tab stop character
Expand All @@ -50,15 +51,13 @@ function format(content, options) {
.value()
if (twoShortestIndent.length === 2) {
originalTabStopChar = twoShortestIndent[1].substring(twoShortestIndent[0].length)
} else {
originalTabStopChar = twoShortestIndent[0] || '\t'
}
originalBaseIndent = twoShortestIndent[0]

// Normalize the original indentation
modifiedContent = 'wrap\n' + originalLines.map(line => {
if (line.trim().length > 0) {
return originalTabStopChar + line.substring(twoShortestIndent[0].length)
return (originalTabStopChar || '\t') + line.substring(twoShortestIndent[0].length)
} else {
return ''
}
Expand Down Expand Up @@ -1041,9 +1040,11 @@ function format(content, options) {
outputLines = outputLines.map(line => line.startsWith(options.tabStopChar) ? line.substring(options.tabStopChar.length) : line)

// Add the original base indentation
if (originalBaseIndent) {
if (originalBaseIndent && originalTabStopChar) {
const outputBaseIndent = _.repeat(options.tabStopChar, originalBaseIndent.length / originalTabStopChar.length)
outputLines = outputLines.map(line => line.trim().length > 0 ? (outputBaseIndent + line) : '')
} else if (originalBaseIndent) {
outputLines = outputLines.map(line => line.trim().length > 0 ? (originalBaseIndent + line) : '')
}
}

Expand Down
4 changes: 4 additions & 0 deletions spec/option-wrap-mode-4/formattingOptions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"wrapMode": true,
"newLineChar": "\r\n"
}
1 change: 1 addition & 0 deletions spec/option-wrap-mode-4/input.styl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
display none
1 change: 1 addition & 0 deletions spec/option-wrap-mode-4/output.styl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
display: none;
4 changes: 4 additions & 0 deletions spec/option-wrap-mode-5/formattingOptions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"wrapMode": true,
"newLineChar": "\r\n"
}
2 changes: 2 additions & 0 deletions spec/option-wrap-mode-5/input.styl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
display none
padding 0
2 changes: 2 additions & 0 deletions spec/option-wrap-mode-5/output.styl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
display: none;
padding: 0;

0 comments on commit bfb5f51

Please sign in to comment.