From: <dal...@us...> - 2024-10-21 19:37:39
|
Revision: 25762 http://sourceforge.net/p/jedit/svn/25762 Author: daleanson Date: 2024-10-21 19:37:36 +0000 (Mon, 21 Oct 2024) Log Message: ----------- add "missing" nodes to the tree so it is easy to find where they need to be inserted into the qdoc Modified Paths: -------------- plugins/QDocSideKick/trunk/src/sidekick/qdoc/QdocNode.java plugins/QDocSideKick/trunk/src/sidekick/qdoc/QdocSideKickParser.java Modified: plugins/QDocSideKick/trunk/src/sidekick/qdoc/QdocNode.java =================================================================== --- plugins/QDocSideKick/trunk/src/sidekick/qdoc/QdocNode.java 2024-10-19 20:27:36 UTC (rev 25761) +++ plugins/QDocSideKick/trunk/src/sidekick/qdoc/QdocNode.java 2024-10-21 19:37:36 UTC (rev 25762) @@ -43,10 +43,6 @@ */ private int ordinal = 0; - private QdocNode parent = null; - - private List<QdocNode> children = null; - // ordinal definitions public static int TITLE = 0; @@ -58,6 +54,10 @@ public static int SECTION4 = 4; + private QdocNode parent = null; + + private List<QdocNode> children = null; + public QdocNode() { super(""); @@ -66,6 +66,11 @@ public QdocNode(String name) { super(name); } + + public QdocNode(String name, int ordinal) { + super(name); + this.ordinal = ordinal; + } public void setStartLocation(Location start) { startLocation = start; Modified: plugins/QDocSideKick/trunk/src/sidekick/qdoc/QdocSideKickParser.java =================================================================== --- plugins/QDocSideKick/trunk/src/sidekick/qdoc/QdocSideKickParser.java 2024-10-19 20:27:36 UTC (rev 25761) +++ plugins/QDocSideKick/trunk/src/sidekick/qdoc/QdocSideKickParser.java 2024-10-21 19:37:36 UTC (rev 25762) @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014, Dale Anson + * Copyright (c) 2024, Dale Anson * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, @@ -52,9 +52,8 @@ public static boolean showAll = false; - public QdocSideKickParser() { - super(NAME); + super( NAME ); } /** @@ -62,8 +61,8 @@ * TODO: is this used anymore? */ public void parse() { - if (currentView != null) { - parse(currentView.getBuffer(), null); + if ( currentView != null ) { + parse( currentView.getBuffer(), null ); } } @@ -75,103 +74,146 @@ * section2's below that and so on. There is nothing in the specification that * requires this, so it is a huge assumption to think this will always be the case, * but that is what I'm assuming here. The sidekick tree could look really funky - * if the qdoc author doesn't follow this hierarchy. + * if the qdoc author doesn't follow this hierarchy. -> Exactly this has been + * reported by Alan, he suggests inserting empty nodes as needed, so, for example, * + * * @param buffer the buffer to parse * @param errorSource where to send any error messages * @return data for the tree */ - public SideKickParsedData parse(Buffer buffer, DefaultErrorSource errorSource) { + public SideKickParsedData parse( Buffer buffer, DefaultErrorSource errorSource ) { String filename = buffer.getPath(); - SideKickParsedData parsedData = new QdocSideKickParsedData(filename); - ImageIcon section1Icon = EclipseIconsPlugin.getIcon("action1.gif"); - ImageIcon section2Icon = EclipseIconsPlugin.getIcon("action2.gif"); - ImageIcon section3Icon = EclipseIconsPlugin.getIcon("action3.gif"); - ImageIcon section4Icon = EclipseIconsPlugin.getIcon("activity.gif"); + SideKickParsedData parsedData = new QdocSideKickParsedData( filename ); + ImageIcon titleIcon = EclipseIconsPlugin.getIcon( "text.gif" ); + ImageIcon section1Icon = EclipseIconsPlugin.getIcon( "action1.gif" ); + ImageIcon section2Icon = EclipseIconsPlugin.getIcon( "action2.gif" ); + ImageIcon section3Icon = EclipseIconsPlugin.getIcon( "action3.gif" ); + ImageIcon section4Icon = EclipseIconsPlugin.getIcon( "activity.gif" ); + ImageIcon missingIcon = EclipseIconsPlugin.getIcon( "delete.gif" ); try { - if (buffer.getLength() <= 0) { + if ( buffer.getLength() <= 0 ) { return parsedData; } int lineCount = buffer.getLineCount(); // end of buffer location/position - Location eofLocation = new Location(lineCount - 1, buffer.getLineEndOffset(lineCount - 1)); - SideKickPosition eofPosition = new SideKickPosition(buffer.getLineEndOffset(lineCount - 1)); - List<QdocNode> nodes = new ArrayList<QdocNode>(); + Location eofLocation = new Location( lineCount - 1, buffer.getLineEndOffset( lineCount - 1 ) ); + SideKickPosition eofPosition = new SideKickPosition( buffer.getLineEndOffset( lineCount - 1 ) ); + // gather the desired nodes here + ArrayList<QdocNode> nodes = new ArrayList<QdocNode>(); + // node info Location startLocation; SideKickPosition startPosition; int length; - // there should only be one title node, but of course, the qdoc spec - // doesn't enforce that, and doesn't require that there is a title node. - // Alan says there can be multiple titles, they should immediately follow - // a \page - QdocNode titleNode = null; - // parse the buffer - for (int lineNumber = 0; lineNumber < lineCount; lineNumber++) { - String lineText = buffer.getLineText(lineNumber); - int index = lineText.indexOf("\\section"); + // parse the buffer - read the buffer line by line, look for \title and \section, + // put them in a list to keep the order of appearance + for ( int lineNumber = 0; lineNumber < lineCount; lineNumber++ ) { + String lineText = buffer.getLineText( lineNumber ); + int index = lineText.indexOf( "\\section" ); length = "\\sectionx".length(); - if (index == -1) { - index = lineText.indexOf("\\title"); + if ( index == -1 ) { + index = lineText.indexOf( "\\title" ); length = "\\title".length(); } - if (index > -1) { - String section = lineText.substring(index, index + length); - String title = lineText.substring(index + length, lineText.length()); - startLocation = new Location(lineNumber, index); - startPosition = new SideKickPosition(buffer.getLineStartOffset(lineNumber) + index); - QdocNode node = new QdocNode(title); - node.setStartLocation(startLocation); - node.setStartPosition(startPosition); - node.setEndLocation(eofLocation); - node.setEndPosition(eofPosition); - switch (section) { + if ( index > -1 ) { + String section = lineText.substring( index, length ); + String text = lineText.substring( index + length, lineText.length() ); + startLocation = new Location( lineNumber, index ); + startPosition = new SideKickPosition( buffer.getLineStartOffset( lineNumber ) + index ); + QdocNode node = new QdocNode( text ); + node.setStartLocation( startLocation ); + node.setStartPosition( startPosition ); + node.setEndLocation( eofLocation ); + node.setEndPosition( eofPosition ); + switch ( section ) { case "\\title": - - if (titleNode == null) { - node.setIcon(null); - node.setOrdinal(TITLE); - titleNode = node; - } + node.setIcon( titleIcon ); + node.setOrdinal( TITLE ); break; case "\\section1": - node.setIcon(section1Icon); - node.setOrdinal(SECTION1); + node.setIcon( section1Icon ); + node.setOrdinal( SECTION1 ); break; case "\\section2": - node.setIcon(section2Icon); - node.setOrdinal(SECTION2); + node.setIcon( section2Icon ); + node.setOrdinal( SECTION2 ); break; case "\\section3": - node.setIcon(section3Icon); - node.setOrdinal(SECTION3); + node.setIcon( section3Icon ); + node.setOrdinal( SECTION3 ); break; case "\\section4": - node.setIcon(section4Icon); - node.setOrdinal(SECTION4); + node.setIcon( section4Icon ); + node.setOrdinal( SECTION4 ); break; } - nodes.add(node); + nodes.add( node ); } } + /* check for missing nodes -- for example, maybe the list looks like this: + * \title text + * \section2 text + * + * insert dummy nodes so it looks like this: + * \title text + * \section1 <missing section 1> + * \section2 text + */ + // make sure there is a title node + if ( nodes.get( 0 ).getOrdinal() != 0 ) { + QdocNode missingTitle = new QdocNode("<missing title>"); + missingTitle.setIcon(missingIcon); + missingTitle.setOrdinal(QdocNode.TITLE); + nodes.add( 0, missingTitle ); + startLocation = new Location( 0, 0 ); + startPosition = new SideKickPosition( 0 ); + missingTitle.setStartLocation( startLocation ); + missingTitle.setStartPosition( startPosition ); + missingTitle.setEndLocation( eofLocation ); + missingTitle.setEndPosition( eofPosition ); + } + + ArrayList<QdocNode> newNodes = new ArrayList<QdocNode>(); + for ( int i = 0; i < nodes.size() - 1; i++ ) { + QdocNode current = nodes.get( i ); + newNodes.add(current); + QdocNode next = nodes.get( i + 1 ); + if ( current.getOrdinal() == next.getOrdinal() || current.getOrdinal() == next.getOrdinal() + 1 ) { + continue; + } + + for ( int j = current.getOrdinal() + 1; j < next.getOrdinal(); j++ ) { + QdocNode missingSection = new QdocNode("<missing section " + j + ">"); + missingSection.setIcon(missingIcon); + missingSection.setOrdinal(j); + newNodes.add( missingSection ); + missingSection.setStartLocation( current.getStartLocation() ); + missingSection.setStartPosition( current.getStartPosition() ); + missingSection.setEndLocation( eofLocation ); + missingSection.setEndPosition( eofPosition ); + } + } + newNodes.add(nodes.get(nodes.size() - 1)); + nodes = newNodes; + // reset the end location/position of the nodes - for (int i = 0; i < nodes.size(); i++) { - QdocNode node = nodes.get(i); - // get the ordinal of the current node + for ( int i = 0; i < nodes.size(); i++ ) { + QdocNode node = nodes.get( i ); int ordinal = node.getOrdinal(); // go down the list to find the next node with the same or smaller ordinal - for (int j = i + 1; j < nodes.size(); j++) { - QdocNode nextNode = nodes.get(j); + for ( int j = i + 1; j < nodes.size(); j++ ) { + QdocNode nextNode = nodes.get( j ); - if (nextNode.getOrdinal() <= ordinal) { + if ( nextNode.getOrdinal() <= ordinal ) { // set the end location/position of the current node to be the same as the // start location/position of the next node. - node.setEndLocation(nextNode.getStartLocation()); - node.setEndPosition(nextNode.getStartPosition()); + node.setEndLocation( nextNode.getStartLocation() ); + node.setEndPosition( nextNode.getStartPosition() ); break; } } @@ -178,7 +220,7 @@ } // arrange the parent/child relationship of the nodes - if (nodes.size() > 0) { + if ( nodes.size() > 0 ) { Deque<QdocNode> deck = new ArrayDeque<QdocNode>(); QdocNode previous0 = null; QdocNode previous1 = null; @@ -185,94 +227,82 @@ QdocNode previous2 = null; QdocNode previous3 = null; - for ( QdocNode node : nodes) { - if (node.getOrdinal() == TITLE) { + for ( QdocNode node : nodes ) { + if ( node.getOrdinal() == TITLE ) { previous0 = node; - deck.add(node); + deck.add( node ); continue; } - if (node.getOrdinal() == SECTION1) { - if (previous0 != null) { - previous0.addChild(node); + if ( node.getOrdinal() == SECTION1 ) { + if ( previous0 != null ) { + previous0.addChild( node ); } else { - deck.add(node); + deck.add( node ); } previous1 = node; continue; } - if (node.getOrdinal() == SECTION2) { - if (previous1 != null) { - previous1.addChild(node); + if ( node.getOrdinal() == SECTION2 ) { + if ( previous1 != null ) { + previous1.addChild( node ); } else { - deck.add(node); + deck.add( node ); } previous2 = node; continue; } - if (node.getOrdinal() == SECTION3) { - if (previous2 != null) { - previous2.addChild(node); + if ( node.getOrdinal() == SECTION3 ) { + if ( previous2 != null ) { + previous2.addChild( node ); } else { - deck.add(node); + deck.add( node ); } previous3 = node; continue; } - if (node.getOrdinal() == SECTION4) { - if (previous3 != null) { - previous3.addChild(node); + if ( node.getOrdinal() == SECTION4 ) { + if ( previous3 != null ) { + previous3.addChild( node ); } else { - deck.add(node); + deck.add( node ); } continue; } } - if (titleNode == null) { - String title = "Untitled"; - startLocation = new Location(0, 0); - startPosition = new SideKickPosition(buffer.getLineStartOffset(0)); - titleNode = new QdocNode(title); - titleNode.setStartLocation(startLocation); - titleNode.setStartPosition(startPosition); - titleNode.setEndLocation(eofLocation); - titleNode.setEndPosition(eofPosition); - } // make a tree for sidekick out of the nodes DefaultMutableTreeNode root = parsedData.root; - for ( QdocNode node : deck) { - DefaultMutableTreeNode treeNode = new DefaultMutableTreeNode(node); - root.add(treeNode); - - if (node.hasChildren()) { - addChildNodes(treeNode); + for ( QdocNode node : deck ) { + DefaultMutableTreeNode treeNode = new DefaultMutableTreeNode( node ); + root.add( treeNode ); + if ( node.hasChildren() ) { + addChildNodes( treeNode ); } } } } - catch ( Exception e) { + catch ( Exception e ) { e.printStackTrace(); } return parsedData; } - private void addChildNodes(DefaultMutableTreeNode parent) { - QdocNode node = (QdocNode) parent.getUserObject(); + private void addChildNodes( DefaultMutableTreeNode parent ) { + QdocNode node = ( QdocNode ) parent.getUserObject(); + for ( QdocNode child : node.getChildren() ) { + DefaultMutableTreeNode childTreeNode = new DefaultMutableTreeNode( child ); + parent.add( childTreeNode ); - for ( QdocNode child : node.getChildren()) { - DefaultMutableTreeNode childTreeNode = new DefaultMutableTreeNode(child); - parent.add(childTreeNode); - - if (child.hasChildren()) { - addChildNodes(childTreeNode); + if ( child.hasChildren() ) { + addChildNodes( childTreeNode ); } } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |