-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
300 additions
and
19 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
const { asyncActionSetup } = require("../../action-setup"); | ||
const { getNewSourceEdit } = require("../../edit-utils/SourceEdit"); | ||
const { transformLocationToRange } = require("../../edit-utils/textEditTransforms"); | ||
const { getSourceSelection } = require("../../source-utilities"); | ||
const { validateUserInput } = require("../../validatorService"); | ||
|
||
function outdent() { | ||
let actionSetup = null; | ||
let sourceSelection = null; | ||
let indentedSelection = null; | ||
return asyncActionSetup() | ||
.then((newActionSetup) => { | ||
actionSetup = newActionSetup; | ||
}) | ||
|
||
.then(() => getSourceSelection(actionSetup.source, actionSetup.location)) | ||
.then((sourceSelection) => validateUserInput({ | ||
value: sourceSelection, | ||
validator: sourceSelection => | ||
sourceSelection | ||
.split(/\r?\n/g) | ||
.reduce((result, line) => | ||
result && /^\s*(")?/.test(line)), | ||
message: "Invalid body selection: not all lines begin with an open quote" | ||
})) | ||
.then((newSourceSelection) => sourceSelection = newSourceSelection) | ||
|
||
.then(() => indentedSelection = sourceSelection | ||
.split(/\r?\n/g) | ||
.map((line) => line.replace(/^(\s*)("\\t)/, '$1\"')) | ||
.join('\n')) | ||
|
||
.then(() => { | ||
const replacementRange = transformLocationToRange(actionSetup.location); | ||
|
||
return getNewSourceEdit() | ||
.addReplacementEdit(replacementRange, indentedSelection) | ||
.applyEdit() | ||
}) | ||
|
||
.catch(function (error) { | ||
console.log(error); | ||
}); | ||
} | ||
|
||
module.exports = { | ||
outdent | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const { WorkspaceEdit, window, workspace } = require("../vscode-service").getVscode(); | ||
|
||
class SourceEdit{ | ||
constructor(){ | ||
this.workspaceEdit = new WorkspaceEdit(); | ||
this.uri = window.activeTextEditor.document.uri; | ||
} | ||
|
||
addInsertEdit(insertPosition, textToInsert) { | ||
this.workspaceEdit.insert(this.uri, insertPosition, textToInsert); | ||
|
||
return this; | ||
} | ||
|
||
addReplacementEdit(replacementRange, replacementText) { | ||
this.workspaceEdit.replace(this.uri, replacementRange, replacementText); | ||
|
||
return this; | ||
} | ||
|
||
applyEdit(){ | ||
return workspace.applyEdit(this.workspaceEdit); | ||
} | ||
} | ||
|
||
function getNewSourceEdit() { | ||
return new SourceEdit(); | ||
} | ||
|
||
module.exports = { | ||
getNewSourceEdit, | ||
SourceEdit | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
function transformSelectionToLocation(selection) { | ||
return { | ||
start: { | ||
line: selection.start.line + 1, | ||
column: selection.start.character | ||
}, | ||
end: { | ||
line: selection.end.line + 1, | ||
column: selection.end.character | ||
} | ||
}; | ||
} | ||
|
||
function transformLocationToSelection(location) { | ||
return { | ||
start: { | ||
line: location.start.line - 1, | ||
character: location.start.column | ||
}, | ||
end: { | ||
line: location.end.line - 1, | ||
character: location.end.column | ||
} | ||
}; | ||
} | ||
|
||
module.exports = { | ||
transformLocationToSelection, | ||
transformSelectionToLocation | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
const vscodeService = require('../vscode-service'); | ||
|
||
const { | ||
Position, | ||
Range, | ||
} = vscodeService.getVscode(); | ||
|
||
|
||
function transformLocationPartToPosition({ line, column }) { | ||
return new Position(line - 1, column); | ||
} | ||
|
||
|
||
function transformLocationToRange({ start, end }) { | ||
return new Range( | ||
transformLocationPartToPosition(start), | ||
transformLocationPartToPosition(end) | ||
); | ||
} | ||
|
||
|
||
function buildEditLocations({ | ||
actionSetup: { location: selectionLocation }, | ||
extractionLocation | ||
}) { | ||
const extractionLocationStart = extractionLocation.start; | ||
|
||
return { | ||
extractionPosition: transformLocationPartToPosition(extractionLocationStart), | ||
replacementRange: transformLocationToRange(selectionLocation) | ||
} | ||
|
||
} | ||
|
||
|
||
module.exports = { | ||
buildEditLocations, | ||
transformLocationPartToPosition, | ||
transformLocationToRange | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
function locationToSourceSelection({ start, end }) { | ||
return { | ||
startLine: start.line - 1, | ||
endLine: end.line - 1, | ||
startColumn: start.column, | ||
endColumn: end.column | ||
}; | ||
} | ||
|
||
function getSourceSelection(sourceCode, location) { | ||
const { | ||
startLine, | ||
endLine, | ||
startColumn, | ||
endColumn | ||
} = locationToSourceSelection(location); | ||
|
||
const selectedLines = sourceCode.split('\n').slice(startLine, endLine + 1); | ||
|
||
if (selectedLines.length === 1) { | ||
return selectedLines[0].slice(startColumn, endColumn) | ||
} else { | ||
const lastIndex = selectedLines.length - 1; | ||
|
||
selectedLines[0] = selectedLines[0].slice(startColumn); | ||
selectedLines[lastIndex] = selectedLines[lastIndex].slice(0, endColumn); | ||
|
||
return selectedLines.join('\n'); | ||
} | ||
|
||
} | ||
|
||
module.exports = { | ||
getSourceSelection, | ||
locationToSourceSelection | ||
}; |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
function validateUserInput({ | ||
value, | ||
validator = () => false, | ||
message = 'Unable to validate user input' | ||
}) { | ||
if (typeof value === 'undefined' || !validator(value)) { | ||
throw new Error(message); | ||
} | ||
|
||
return value; | ||
} | ||
|
||
module.exports = { | ||
validateUserInput | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,18 @@ | ||
function getVsCode() { | ||
return require('vscode'); | ||
let vscode = null; | ||
|
||
function setVscodeInstance(vscodeInstance) { | ||
vscode = vscodeInstance; | ||
} | ||
|
||
function getVscode() { | ||
if(vscode === null) { | ||
vscode = require('vscode'); | ||
} | ||
|
||
return vscode; | ||
} | ||
|
||
module.exports = { | ||
getVsCode | ||
} | ||
setVscodeInstance, | ||
getVscode | ||
}; |
This file contains 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