Better way to use Velocity's GenericTools in a Standalone app? - velocity

I want to use VelocityTool's GenericTools for some standard formatting in a standalone app. e.g. have something like this in my Velocity template to use the GenericTools' NumberTool formatter:
Total: $numberTool.format("#0.00", $totalPnL)
How do I associate the above "$numberTool" with the GenericTool NumberTool. Here's my Velocity code:
Velocity.init();
VelocityContext velocityContext = new VelocityContext();
Template template = Velocity.getTemplate("example.vm");
velocityContext.put("totalPnL", 100);
StringWriter sw = new StringWriter();
template.merge(velocityContext, sw);
Now I know I can do this to get it to work:
velocityContext.put("numberTool", new NumberTool());
But is that how I need to add all the GenericTools to my app? Manually and one at a time (e.g. another line for DateTool ... etc)? Isn't there a way to make all the GenericTools exposed to my template with out this? I know there's a "tools.xml" that comes with VelocityTools that has the GenericTools defined. Can I just add that to my app to expose all the tools? If so, how?
thanks,
David

http://velocity.apache.org/tools/devel/javadoc/org/apache/velocity/tools/ToolManager.html
http://velocity.apache.org/tools/devel/standalone.html
The default tool configuration provides all the generic tools already. Though you can create a config if you want to configure those tools. There's even auto loading for configurations, or manual specification.
ToolManager tm = new ToolManager();
tm.setVelocityEngine(yourVelocityEngine);
Context context = tm.createContext();

it is at least the way I do it too.
I'll put for example
context.put("esc", new EscapeTool());
and in the template I simply use then
${esc.h}
to write a "#" in the code so that Velocity does not parse it as "velocity-script".
I think those helper tools are rather utils and only cover some basic signs. They are not intend to be a standard, you rather can include them on-demand.
I've build for example an abstract class that loads the context of velocity and puts the EscapeTool into the context all the time so that I do not have to add it everywhere.
Good luck with your project
Sebastian

Related

Documentation for Xcode Source Editor Extension

I'm looking for some documentation of the new Xcode Source Editor Extensions in Xcode 8.
As far as I can see there is only the "documentation" found in the header file for XcodeKit. Would be great to get something that's more detailed and more official.
Very preliminary XcodeKit reference documentation is now available.
Our WWDC 2016 presentation introducing Xcode Source Editor Extensions remains the best walkthrough.
The very shortest version, however, is: Because App Extensions need to be embedded in an application, you need to first create a new macOS Cocoa Application, and then add a new Xcode Source Editor Extension to that application. Then the XcodeKit reference should help some in implementing that.
Not really a documentation but a good reference also
https://developer.apple.com/videos/play/wwdc2016/414/
Extensions, at the moment, are poorly documented. There are a lot of assumptions made (for example, did you know that you can execute the container app? Yup, it’s really nice for settings GUI - see this How To Execute Container App - Second Answer)
At the moment, there are a lot of things missing: for example, there isn’t a structure that shows the corresponding lines with the data object - though this is quickly created with the following code:
var matches: [NSTextCheckingResult] = []
do {
let regex = try NSRegularExpression(pattern: "\n", options: [])
matches = regex.matches(in: completeBuffer,
options: [],
range: NSMakeRange(0, completeBuffer.count))
}
catch {
}
This gives you the location of all the \n’s - you should be able to fill out the rest to give you starting and ending positions which should match up to the lines.
All in all, there is a lot to like about the extension, but there are quite a few things missing as well.
Currently the only available documentation is in the headers; there's nothing "unofficial" about them. If you have specific questions, please ask.

IntelliJ IDEA plugin development: how to modify the Psi tree?

I would like to know what the "proper" way to create new PsiElement instances and add them to the Psi tree is.
I looked at the tutorial provided by JetBrains (http://www.jetbrains.org/intellij/sdk/docs/tutorials/custom_language_support/quick_fix.html). The method they use there seems suitable for the simple properties language they introduce but I don't think it's the way to go with a more complicated syntax, where I want to add a child PsiElement that cannot appear at the root level of a PsiFile.
What would be best for me is to be able to parse a text snippet as an element and have it added to the tree. I tried to do something like this:
myLangTopElement.getNode().addLeaf(MyLangTypes.CHILD_EXPRESSION, "fish = '42'", null);
It actually seems to work - the text is added to the document and a node is created but when I edit the text inside the quotes in the editor - some exceptions are thrown...
What am I doing wrong? What is the correct way to add new elements?
PSI is complicated :(. A usual way is to create a whole file from a carefully prepared text (PsiFileFactory#createFileFromText), extract from it the PSI element you need to add into the tree, and then call PsiElement#add/addBefore/addAfter passing the extracted element as an argument. If your PSI element is a wrapper over AST (i.e. AstDelegatePsiElement), its add* methods already do the magic necessary for the exceptions not to be thrown.
You can study GrCreateSubclassAction#startTemplate from the IDEA CE source for an example, and the implementation of createCodeReferenceElementFromText that it calls.

How do I include a custom field client control JS file?

I'm trying to develop a custom field using the steps outlined here: http://www.sitefinity.com/documentation/documentationarticles/developers-guide/sitefinity-essentials/controls/types-of-controls/field-controls/building-a-custom-field-control
Unfortunately, I don't know how/where to include the client control js file, and it doesn't seem to say in the documentation. Can anyone explain how I might do this?
I'm very new to Sitefinity dev (as in this is the first thing I've done) and web dev in general, so feel free to suggest things that "should be obvious"
I also asked this question on the Sitefinity forums - I'll be sure to copy the answer here if I get one there.
Ok, got it.
First you need to set the processing options for the js file to Embedded Resource
Then add it to the AssemblyInfo.cs:
[assembly: WebResource("namespace.filename.js", "text/javascript")]
Then override the GetScriptReferences method in the class that inherits from FieldControl:
public override IEnumerable<ScriptReference> GetScriptReferences()
{
var baseReferences = new List<ScriptReference>(base.GetScriptReferences());
var newRef = new ScriptReference(javascriptPath, this.GetType().Assembly.FullName);
baseReferences.Add(newRef);
return baseReferences;
}

How to expose the content formatter in a custom Eclipse editor?

I am writing a custom Eclipse editor by subclassing TextEditor, and I can't use the Format action that I configured.
I read the 3 parts in Creating a commercial quality IDE, and I know about SourceViewerConfiguration. I implemented the required method:
override def getContentFormatter(viewer: ISourceViewer) = {
val formatter = new MultiPassContentFormatter(getConfiguredDocumentPartitioning(viewer), IDocument.DEFAULT_CONTENT_TYPE)
formatter.setMasterStrategy(new ScalaFormattingStrategy(textEditor))
formatter
}
However, I can't find Format anywhere in the menu, contextual menu, toolbar, etc. The Java shortcut (CMD-Shift-F) does not work either.
Edit: I have implemented other methods in the SourceViewerConfiguration subclass I created, and everything else works as expected in my editor (completion, hyperlinking, reconciliation).
What is the preferred way to expose the formatter? Do I need to do anything more?
Quoting the Eclipse formatter FAQ:
Finally, you will need to create an action that invokes the formatter.
No generic formatting action is defined by the text infrastructure,
but it is quite easy to create one of your own. The action’s run
method can simply call the following on the source viewer to invoke
the formatter:
sourceViewer.doOperation(ISourceViewer.FORMAT);

Trigger Eclipse's code formatter programmatically from a new file wizard

I'm writing an Eclipse plugin with a wizard (org.eclipse.jface.wizard.Wizard) which creates a new file with a basic code template. To simplify the "piecing together" of the file contents, I plan to stuff everything into one long string, inject it into the file, and then call my custom Formatter (inherits org.eclipse.xtext.formatting.impl.AbstractDeclarativeFormatter) to clean up all the indentation and so on.
Question is, how do I go about calling the formatter programmatically?
In the wizard I call IDE.openEditor() and get back a handle to an IEditorPart. What can I do from here?
Well, I have found my answer:
IEditorPart editor = IDE.openEditor(page, file, true);
XtextEditor xed = (XtextEditor)editor;
((SourceViewer)xed.getInternalSourceViewer()).doOperation(ISourceViewer.FORMAT);
Maybe that will help someone else looking for something similar!