-
-
Notifications
You must be signed in to change notification settings - Fork 243
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
14 changed files
with
293 additions
and
148 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
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
85 changes: 85 additions & 0 deletions
85
...com.python.pydev.analysis/src/com/python/pydev/analysis/refactoring/tdd/TemplateInfo.java
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,85 @@ | ||
package com.python.pydev.analysis.refactoring.tdd; | ||
|
||
import org.eclipse.jface.text.IDocument; | ||
import org.eclipse.jface.text.Region; | ||
import org.eclipse.jface.text.templates.DocumentTemplateContext; | ||
import org.eclipse.jface.text.templates.Template; | ||
import org.eclipse.jface.text.templates.TemplateBuffer; | ||
import org.eclipse.jface.text.templates.TemplateException; | ||
import org.python.pydev.core.log.Log; | ||
import org.python.pydev.core.templates.PyDocumentTemplateContext; | ||
|
||
public class TemplateInfo { | ||
|
||
public Template fTemplate; | ||
public PyDocumentTemplateContext fContext; | ||
public Region fRegion; | ||
|
||
public TemplateInfo(Template template, PyDocumentTemplateContext context, Region region) { | ||
this.fTemplate = template; | ||
this.fContext = context; | ||
this.fRegion = region; | ||
} | ||
|
||
/** | ||
* Returns the offset of the range in the document that will be replaced by | ||
* applying this template. | ||
* | ||
* @return the offset of the range in the document that will be replaced by | ||
* applying this template | ||
* @since 3.1 | ||
*/ | ||
protected final int getReplaceOffset() { | ||
int start; | ||
if (fContext instanceof DocumentTemplateContext) { | ||
DocumentTemplateContext docContext = fContext; | ||
start = docContext.getStart(); | ||
} else { | ||
start = fRegion.getOffset(); | ||
} | ||
return start; | ||
} | ||
|
||
/** | ||
* Returns the end offset of the range in the document that will be replaced | ||
* by applying this template. | ||
* | ||
* @return the end offset of the range in the document that will be replaced | ||
* by applying this template | ||
* @since 3.1 | ||
*/ | ||
protected final int getReplaceEndOffset() { | ||
int end; | ||
if (fContext instanceof DocumentTemplateContext) { | ||
DocumentTemplateContext docContext = fContext; | ||
end = docContext.getEnd(); | ||
} else { | ||
end = fRegion.getOffset() + fRegion.getLength(); | ||
} | ||
return end; | ||
} | ||
|
||
public void apply(IDocument document) { | ||
try { | ||
fContext.setReadOnly(false); | ||
int start; | ||
TemplateBuffer templateBuffer; | ||
try { | ||
// this may already modify the document (e.g. add imports) | ||
templateBuffer = fContext.evaluate(fTemplate); | ||
} catch (TemplateException e1) { | ||
return; | ||
} | ||
|
||
start = getReplaceOffset(); | ||
int end = getReplaceEndOffset(); | ||
|
||
// insert template string | ||
String templateString = templateBuffer.getString(); | ||
document.replace(start, end - start, templateString); | ||
} catch (Exception e) { | ||
Log.log(e); | ||
} | ||
} | ||
|
||
} |
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
Oops, something went wrong.