|
24 | 24 | import java.util.Stack; |
25 | 25 |
|
26 | 26 | import org.eclipse.swt.SWT; |
| 27 | +import org.eclipse.swt.custom.StyleRange; |
27 | 28 | import org.eclipse.swt.custom.StyledText; |
28 | 29 | import org.eclipse.swt.graphics.Point; |
29 | 30 | import org.eclipse.swt.graphics.Rectangle; |
|
46 | 47 | import org.eclipse.jface.text.IAutoEditStrategy; |
47 | 48 | import org.eclipse.jface.text.IBlockTextSelection; |
48 | 49 | import org.eclipse.jface.text.IDocument; |
| 50 | +import org.eclipse.jface.text.IDocumentExtension3; |
49 | 51 | import org.eclipse.jface.text.IDocumentExtension4; |
| 52 | +import org.eclipse.jface.text.IDocumentPartitioner; |
50 | 53 | import org.eclipse.jface.text.IPositionUpdater; |
51 | 54 | import org.eclipse.jface.text.IRegion; |
52 | 55 | import org.eclipse.jface.text.IRewriteTarget; |
|
56 | 59 | import org.eclipse.jface.text.ITextSelection; |
57 | 60 | import org.eclipse.jface.text.ITextViewerExtension2; |
58 | 61 | import org.eclipse.jface.text.ITextViewerLifecycle; |
| 62 | +import org.eclipse.jface.text.ITypedRegion; |
59 | 63 | import org.eclipse.jface.text.Position; |
60 | 64 | import org.eclipse.jface.text.Region; |
| 65 | +import org.eclipse.jface.text.TextPresentation; |
| 66 | +import org.eclipse.jface.text.TextUtilities; |
61 | 67 | import org.eclipse.jface.text.TextViewer; |
62 | 68 | import org.eclipse.jface.text.codemining.ICodeMiningProvider; |
63 | 69 | import org.eclipse.jface.text.contentassist.IContentAssistant; |
|
71 | 77 | import org.eclipse.jface.text.hyperlink.IHyperlinkDetector; |
72 | 78 | import org.eclipse.jface.text.information.IInformationPresenter; |
73 | 79 | import org.eclipse.jface.text.presentation.IPresentationReconciler; |
| 80 | +import org.eclipse.jface.text.presentation.IPresentationReconcilerExtension; |
| 81 | +import org.eclipse.jface.text.presentation.IPresentationRepairer; |
74 | 82 | import org.eclipse.jface.text.projection.ChildDocument; |
75 | 83 | import org.eclipse.jface.text.quickassist.IQuickAssistAssistant; |
76 | 84 | import org.eclipse.jface.text.quickassist.IQuickAssistInvocationContext; |
@@ -1393,4 +1401,77 @@ private void uninstallTextViewer() { |
1393 | 1401 | lifecycles.clear(); |
1394 | 1402 | } |
1395 | 1403 |
|
| 1404 | + /** |
| 1405 | + * Computes syntax-highlighting style ranges for a region of an external document using this |
| 1406 | + * viewer's configured presentation reconciler and partitioner. |
| 1407 | + * <p> |
| 1408 | + * This is useful when you want to syntax-color content that is <em>not</em> the document |
| 1409 | + * currently displayed in the viewer — for example, to preview or render a snippet with the same |
| 1410 | + * language rules that are active in this viewer. |
| 1411 | + * </p> |
| 1412 | + * <p> |
| 1413 | + * The viewer's partitioner is temporarily connected to the given document so that its |
| 1414 | + * presentation repairers can compute the correct highlighting. The viewer's own document is not |
| 1415 | + * affected. |
| 1416 | + * </p> |
| 1417 | + * |
| 1418 | + * @param document the external document whose content should be highlighted; must not be |
| 1419 | + * {@code null} and must use the same language/content type as this viewer |
| 1420 | + * @param damage the region within {@code document} for which style ranges are computed; must |
| 1421 | + * not be {@code null} |
| 1422 | + * @return the list of {@link org.eclipse.swt.custom.StyleRange}s covering the given region, as |
| 1423 | + * produced by this viewer's presentation reconciler; never {@code null}, may be empty |
| 1424 | + * if no repairer is registered for the content type |
| 1425 | + * @throws BadLocationException if {@code damage} is outside the bounds of {@code document} |
| 1426 | + * @since 3.31 |
| 1427 | + */ |
| 1428 | + public List<StyleRange> computeStyleRanges(IDocument document, IRegion damage) throws BadLocationException { |
| 1429 | + String partition= IDocumentExtension3.DEFAULT_PARTITIONING; |
| 1430 | + IPresentationReconciler reconciler= fPresentationReconciler; |
| 1431 | + if (reconciler instanceof IPresentationReconcilerExtension ext) { |
| 1432 | + String extPartition= ext.getDocumentPartitioning(); |
| 1433 | + if (extPartition != null && !extPartition.isEmpty()) { |
| 1434 | + partition= extPartition; |
| 1435 | + } |
| 1436 | + } |
| 1437 | + IDocument originalDocument= getDocument(); |
| 1438 | + IDocumentPartitioner partitioner= originalDocument.getDocumentPartitioner(); |
| 1439 | + document.setDocumentPartitioner(partitioner); |
| 1440 | + IDocumentPartitioner originalDocumentPartitioner= null; |
| 1441 | + if (document instanceof IDocumentExtension3 ext |
| 1442 | + && originalDocument instanceof IDocumentExtension3 originalExt) { |
| 1443 | + originalDocumentPartitioner= originalExt.getDocumentPartitioner(partition); |
| 1444 | + if (originalDocumentPartitioner != null) { |
| 1445 | + // set temporarily another document in partitioner so that presentation can be |
| 1446 | + // created for given source |
| 1447 | + originalDocumentPartitioner.disconnect(); |
| 1448 | + try { |
| 1449 | + originalDocumentPartitioner.connect(document); |
| 1450 | + } finally { |
| 1451 | + ext.setDocumentPartitioner(partition, originalDocumentPartitioner); |
| 1452 | + } |
| 1453 | + } |
| 1454 | + } |
| 1455 | + TextPresentation presentation= new TextPresentation(damage, 1000); |
| 1456 | + ITypedRegion[] partitioning= TextUtilities.computePartitioning(document, partition, damage.getOffset(), |
| 1457 | + damage.getLength(), false); |
| 1458 | + for (ITypedRegion r : partitioning) { |
| 1459 | + IPresentationRepairer repairer= reconciler.getRepairer(r.getType()); |
| 1460 | + if (repairer != null) { |
| 1461 | + repairer.setDocument(document); |
| 1462 | + repairer.createPresentation(presentation, r); |
| 1463 | + repairer.setDocument(originalDocument); |
| 1464 | + } |
| 1465 | + } |
| 1466 | + if (originalDocumentPartitioner != null) { |
| 1467 | + originalDocumentPartitioner.connect(originalDocument); |
| 1468 | + } |
| 1469 | + List<StyleRange> result= new ArrayList<>(); |
| 1470 | + var it= presentation.getAllStyleRangeIterator(); |
| 1471 | + while (it.hasNext()) { |
| 1472 | + StyleRange next= it.next(); |
| 1473 | + result.add(next); |
| 1474 | + } |
| 1475 | + return result; |
| 1476 | + } |
1396 | 1477 | } |
0 commit comments