本文整理汇总了Java中org.netbeans.modules.editor.indent.spi.Context类的典型用法代码示例。如果您正苦于以下问题:Java Context类的具体用法?Java Context怎么用?Java Context使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Context类属于org.netbeans.modules.editor.indent.spi包,在下文中一共展示了Context类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: reformat
import org.netbeans.modules.editor.indent.spi.Context; //导入依赖的package包/类
public static String reformat(String text, CodeStyle style, int rightMargin) {
StringBuilder sb = new StringBuilder(text);
ClassPath empty = ClassPathSupport.createClassPath(new URL[0]);
ClasspathInfo cpInfo = ClasspathInfo.create(JavaPlatformManager.getDefault().getDefaultPlatform().getBootstrapLibraries(), empty, empty);
JavacTaskImpl javacTask = JavacParser.createJavacTask(cpInfo, null, null, null, null, null, null, null, Arrays.asList(FileObjects.memoryFileObject("","Scratch.java", text)));
com.sun.tools.javac.util.Context ctx = javacTask.getContext();
JavaCompiler.instance(ctx).genEndPos = true;
CompilationUnitTree tree = javacTask.parse().iterator().next(); //NOI18N
SourcePositions sp = JavacTrees.instance(ctx).getSourcePositions();
TokenSequence<JavaTokenId> tokens = TokenHierarchy.create(text, JavaTokenId.language()).tokenSequence(JavaTokenId.language());
for (Diff diff : Pretty.reformat(text, tokens, new TreePath(tree), sp, style, rightMargin)) {
int start = diff.getStartOffset();
int end = diff.getEndOffset();
sb.delete(start, end);
String t = diff.getText();
if (t != null && t.length() > 0) {
sb.insert(start, t);
}
}
return sb.toString();
}
示例2: handleSmartEnter
import org.netbeans.modules.editor.indent.spi.Context; //导入依赖的package包/类
public boolean handleSmartEnter(Context context) throws BadLocationException {
boolean wasSmartEnter = false;
BaseDocument doc = (BaseDocument)context.document();
int dotPos = context.caretOffset();
wasSmartEnter = isSmartEnter(doc, dotPos);
if (wasSmartEnter) {
int line = Utilities.getLineOffset(doc, dotPos);
assert line > 0;
int baseIndent = getExistingIndent(doc, line - 1);
doc.insertString(dotPos, "\n", null); //NOI18N
Position position = doc.createPosition(dotPos);
context.modifyIndent(Utilities.getRowStartFromLineOffset(doc, line), baseIndent + doc.getShiftWidth());
context.modifyIndent(Utilities.getRowStartFromLineOffset(doc, line + 1), baseIndent);
context.setCaretOffset(position.getOffset());
}
return wasSmartEnter;
}
示例3: HtmlIndenter
import org.netbeans.modules.editor.indent.spi.Context; //导入依赖的package包/类
public HtmlIndenter(Context context) {
super(HTMLTokenId.language(), context);
try {
Document doc = context.document();
FileObject file = DataLoadersBridge.getDefault().getFileObject(doc);
CharSequence code = doc.getText(0, doc.getLength());
HtmlSource source = new HtmlSource(code, null, file);
SyntaxAnalyzerResult result = SyntaxAnalyzer.create(source).analyze();
model = result.getHtmlModel();
HtmlVersion version = result.getHtmlVersion();
//workaround for [Bug 204163] [71cat] wrong formatting
if(version == HtmlVersion.XHTML5) {
//we do not have a special model for xhtml5, just html5 model =>
//use xhtml1.0 model for formatting
version = HtmlVersion.XHTML10_TRANSATIONAL;
}
model = HtmlModelFactory.getModel(version);
} catch (BadLocationException ex) {
Exceptions.printStackTrace(ex);
}
assert model != null;
}
示例4: reformat
import org.netbeans.modules.editor.indent.spi.Context; //导入依赖的package包/类
public void reformat(Context context, ParserResult info) {
final Context formatContext = context;
final BaseDocument doc = (BaseDocument) context.document();
doc.runAtomic(new Runnable() {
@Override
public void run() {
final int caretOffset = EditorRegistry.lastFocusedComponent() != null
? EditorRegistry.lastFocusedComponent().getCaretPosition()
: 0;
MutableTextInput mti = (MutableTextInput) doc.getProperty(MutableTextInput.class);
try {
mti.tokenHierarchyControl().setActive(false);
} finally {
mti.tokenHierarchyControl().setActive(true);
}
}
});
}
示例5: process
import org.netbeans.modules.editor.indent.spi.Context; //导入依赖的package包/类
public void process(Context context) throws BadLocationException{
if (context.isIndent()) {
//
// A temporary workaround for issue #178512
BaseDocument doc = (BaseDocument)context.document();
int firstLine = Utilities.getLineOffset(doc, context.startOffset());
int lastLine = Utilities.getLineOffset(doc, context.endOffset());
if (firstLine == lastLine) {
enterPressed(context);
} else {
reformat(context);
}
} else {
reformat(context);
}
}
示例6: get
import org.netbeans.modules.editor.indent.spi.Context; //导入依赖的package包/类
public static IndentSpiPackageAccessor get() {
if (INSTANCE == null) {
// Enforce the static initializer in Context class to be run
try {
Class.forName(Context.class.getName(), true, Context.class.getClassLoader());
} catch (ClassNotFoundException e) { }
}
return INSTANCE;
}
示例7: reformat
import org.netbeans.modules.editor.indent.spi.Context; //导入依赖的package包/类
public void reformat() throws BadLocationException {
assertEquals(context.startOffset(), 0);
int lineStartOffset = context.lineStartOffset(context.startOffset());
assertEquals(lineStartOffset, context.startOffset());
context.modifyIndent(context.startOffset(), 2);
assertEquals(context.lineIndent(lineStartOffset), 2);
context.modifyIndent(context.startOffset(), 0);
assertEquals(context.lineIndent(lineStartOffset), 0);
List<Context.Region> regions = context.indentRegions();
Context.Region region = regions.get(0);
assertEquals(region.getStartOffset(), 0);
assertEquals(region.getEndOffset(), context.document().getLength());
context.document().insertString(context.startOffset(), TestLineTokenId.MIME_TYPE + "/", null);
reformatPos = context.document().createPosition(context.startOffset());
}
示例8: getIndentTaskFactory
import org.netbeans.modules.editor.indent.spi.Context; //导入依赖的package包/类
public IndentTask.Factory getIndentTaskFactory() {
if (indentTaskFactory == null) {
indentTaskFactory = new IndentTask.Factory() {
public IndentTask createTask(Context context) {
Formatter formatter = getFormatter();
if (formatter != null && context.document() instanceof BaseDocument) {
return new Indenter(context, formatter);
} else {
return null;
}
}
};
}
return indentTaskFactory;
}
示例9: getReformatTaskFactory
import org.netbeans.modules.editor.indent.spi.Context; //导入依赖的package包/类
public ReformatTask.Factory getReformatTaskFactory() {
if (reformatTaskFactory == null) {
reformatTaskFactory = new ReformatTask.Factory() {
public ReformatTask createTask(Context context) {
Formatter formatter = getFormatter();
if (formatter != null && context.document() instanceof BaseDocument) {
return new Reformatter(context, formatter);
} else {
return null;
}
}
};
}
return reformatTaskFactory;
}
示例10: reformat
import org.netbeans.modules.editor.indent.spi.Context; //导入依赖的package包/类
public void reformat(Context context, final int startOffset, final int endOffset)
throws BadLocationException {
final BaseDocument doc = (BaseDocument) context.document();
doc.runAtomic(new Runnable() {
public void run() {
doReformat(doc, startOffset, endOffset);
}
});
}
示例11: documentFrom
import org.netbeans.modules.editor.indent.spi.Context; //导入依赖的package包/类
/**
* @requires context != null
* @return a new Document from the given context.
*/
private static Document documentFrom(Context context) {
try {
return new ProtobufDocument(
context.document()
.getText(ZERO, context.document().getLength()));
} catch (BadLocationException ex) {
throw new FailureException(
"ProtobufIndentTask.documentFrom", ex);
}
}
示例12: ContextIndent
import org.netbeans.modules.editor.indent.spi.Context; //导入依赖的package包/类
/**
* Constructor
*
* @param context the current Apex file.
*/
public ContextIndent(Context context) {
this.context = context;
document = context.document();
tokenHierarchy = TokenHierarchy.get(document);
indentLevelSize = org.netbeans.modules.editor.indent.api.IndentUtils.indentLevelSize(document);
}
示例13: DocumentFormatter
import org.netbeans.modules.editor.indent.spi.Context; //导入依赖的package包/类
/**
* Constructor
*
* @param context the current Apex file.
* @param parserResult contains the tokens sequence of the Apex file
*/
public DocumentFormatter(Context context, ParserResult parserResult) {
this.context = context;
Document document = context.document();
this.parserResult = (ApexParserResult) parserResult;
contextIndent = new ContextIndent(context);
Preferences preferences = CodeStylePreferences.get(document).getPreferences();
bracesFormatter = new BracesFormatter(document, preferences, FormatOptions.getInstance());
newLineFormatter = new NewLineFormatter(document);
reformatTreeVisitor = new ReformatTreeVisitor(document,context.startOffset(), context.endOffset());
optionsToReformat = new ArrayList<>();
visitParserResult();
}
示例14: reindent
import org.netbeans.modules.editor.indent.spi.Context; //导入依赖的package包/类
@Override
public void reindent(Context context) {
ContextIndent contextIndent = new ContextIndent(context);
try {
int indentLevel = contextIndent.findCurrentDepthToIndent(context.startOffset());
context.modifyIndent(context.startOffset(), indentLevel);
} catch (BadLocationException ex) {
LOG.log(Level.SEVERE, ex.toString(), ex);
}
}
示例15: reformat
import org.netbeans.modules.editor.indent.spi.Context; //导入依赖的package包/类
@Override
public void reformat() throws BadLocationException {
if (context.indentRegions().size() > 1) {
throw new UnsupportedOperationException("The reformatter currently only supports one region per call.");
}
for (final Context.Region region : context.indentRegions()) {
final String original = context.document().getText(region.getStartOffset(), region.getEndOffset() - region.getStartOffset());
if (original == null || original.isEmpty()) {
continue;
}
final String formatted = runGofmt(original);
if (formatted == null || formatted.equals(original)) {
continue;
}
Runnable applyer = new Runnable() {
@Override
public void run() {
try {
context.document().remove(region.getStartOffset(), region.getEndOffset() - region.getStartOffset());
context.document().insertString(region.getStartOffset(), formatted, null);
} catch (BadLocationException ex) {
LOGGER.log(Level.WARNING, ex.getLocalizedMessage(), ex);
throw new RuntimeException(ex);
}
}
};
if (context.document() instanceof BaseDocument) {
((BaseDocument)context.document()).runAtomicAsUser(applyer);
} else {
applyer.run();
}
}
}