use of com.reprezen.swagedit.core.editor.JsonDocument in project KaiZen-OpenAPI-Editor by RepreZen.
the class AbstractJsonHyperlinkDetector method detectHyperlinks.
@Override
public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) {
JsonDocument document = (JsonDocument) textViewer.getDocument();
JsonPointer basePath = document.getPath(region);
if (!canDetect(basePath)) {
return null;
}
HyperlinkInfo info = getHyperlinkInfo(textViewer, region);
if (info == null) {
return null;
}
return doDetect(document, textViewer, info, basePath);
}
use of com.reprezen.swagedit.core.editor.JsonDocument in project KaiZen-OpenAPI-Editor by RepreZen.
the class AbstractJsonHyperlinkDetector method getHyperlinkInfo.
protected HyperlinkInfo getHyperlinkInfo(ITextViewer viewer, IRegion region) {
final JsonDocument document = (JsonDocument) viewer.getDocument();
IRegion line;
try {
line = document.getLineInformationOfOffset(region.getOffset());
} catch (BadLocationException e) {
return null;
}
String lineContent;
try {
lineContent = document.get(line.getOffset(), line.getLength());
} catch (BadLocationException e) {
return null;
}
if (lineContent == null || emptyToNull(lineContent) == null) {
return null;
}
final int column = region.getOffset() - line.getOffset();
final IRegion selected = getSelectedRegion(line, lineContent, column);
String text;
try {
text = document.get(selected.getOffset(), selected.getLength());
} catch (BadLocationException e) {
return null;
}
if (emptyToNull(text) == null || text.trim().equals(":") || text.trim().equals("$ref:")) {
return null;
}
return new HyperlinkInfo(selected, text, column);
}
use of com.reprezen.swagedit.core.editor.JsonDocument in project KaiZen-OpenAPI-Editor by RepreZen.
the class JsonContentAssistProcessor method getContextType.
@Override
protected TemplateContextType getContextType(ITextViewer viewer, IRegion region) {
Model model = null;
if (viewer.getDocument() instanceof JsonDocument) {
model = ((JsonDocument) viewer.getDocument()).getModel();
}
String contextType = getContextTypeId(model, currentPath.toString());
ContextTypeRegistry registry = getContextTypeRegistry();
if (registry != null) {
return registry.getContextType(contextType);
} else {
return null;
}
}
use of com.reprezen.swagedit.core.editor.JsonDocument in project KaiZen-OpenAPI-Editor by RepreZen.
the class JsonContentAssistProcessor method computeCompletionProposals.
@Override
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
if (!(viewer.getDocument() instanceof JsonDocument)) {
return super.computeCompletionProposals(viewer, documentOffset);
}
maybeSwitchScope(documentOffset);
final JsonDocument document = (JsonDocument) viewer.getDocument();
final ITextSelection selection = (ITextSelection) viewer.getSelectionProvider().getSelection();
int line = 0, lineOffset = 0, column = 0;
try {
line = document.getLineOfOffset(documentOffset);
lineOffset = document.getLineOffset(line);
column = selection.getOffset() - lineOffset;
} catch (BadLocationException e) {
}
final String prefix = extractPrefix(viewer, documentOffset);
// column to resolve the path
if (!prefix.isEmpty()) {
column -= prefix.length();
}
Model model = document.getModel(documentOffset - prefix.length());
currentPath = model.getPath(line, column);
isRefCompletion = referenceProposalProvider.canProvideProposal(model, currentPath);
Collection<Proposal> p;
if (isRefCompletion) {
updateStatus(model);
p = referenceProposalProvider.getProposals(currentPath, document, currentScope);
} else {
clearStatus();
p = proposalProvider.getProposals(currentPath, model, prefix);
}
final Collection<ICompletionProposal> proposals = getCompletionProposals(p, prefix, documentOffset);
// compute template proposals
if (!isRefCompletion) {
final ICompletionProposal[] templateProposals = super.computeCompletionProposals(viewer, documentOffset);
if (templateProposals != null && templateProposals.length > 0) {
proposals.addAll(Lists.newArrayList(templateProposals));
}
}
return proposals.toArray(new ICompletionProposal[proposals.size()]);
}
use of com.reprezen.swagedit.core.editor.JsonDocument in project KaiZen-OpenAPI-Editor by RepreZen.
the class JsonContentOutlinePage method update.
protected void update() {
final IDocument document = documentProvider.getDocument(currentInput);
if (document instanceof JsonDocument) {
final Model model = ((JsonDocument) document).getModel();
if (model == null) {
return;
}
final TreeViewer viewer = getTreeViewer();
if (viewer != null && viewer.getControl() != null && !viewer.getControl().isDisposed()) {
// we keep all elements that have been previously expanded
// so the tree stay in the same state between updates.
final Object[] expandedElements = viewer.getExpandedElements();
final List<Object> elements = expandedElements != null ? Arrays.asList(expandedElements) : null;
viewer.setInput(model);
if (elements != null && !elements.isEmpty()) {
Iterable<AbstractNode> newElements = filter(model.allNodes(), new Predicate<AbstractNode>() {
@Override
public boolean apply(AbstractNode node) {
return elements.contains(node);
}
});
viewer.setExpandedElements(toArray(newElements, AbstractNode.class));
}
}
}
}
Aggregations