AngularJSHtmlCodeAssistProcessor implements HTMLCodeAssistProcessor { protected AngularJSQuery getQuery(TextEditor textEditor) { EmbeddedDocument document = textEditor.getDocument(); TextPosition cursorPosition = document.getCursorPosition(); int totalNumberOfLines = document.getLineCount(); int line = cursorPosition.getLine(); int column = cursorPosition.getCharacter(); int lineWithCursor = line; String textBefore = ""; boolean parsingLineWithCursor = true; while ((line >= 0) && (!textBefore.contains("<"))) { String text; int lastOpen; if (parsingLineWithCursor) { final String lineContent = document.getLineContent(line); text = lineContent.substring(0, column); parsingLineWithCursor = false; } else { text = document.getLineContent(line); } textBefore = text.concat(textBefore); lastOpen = text.lastIndexOf('<'); if (lastOpen == -1) { line--; } } if (textBefore.startsWith("</")) { return null; } String textAfter = ""; line = lineWithCursor; while (line < totalNumberOfLines && !textAfter.contains(">")) { int lastOpen; int lastClose; String text; final String lineContent = document.getLineContent(line); if (parsingLineWithCursor) { text = lineContent.substring(column); parsingLineWithCursor = false; } else { text = lineContent.trim(); } String newText = textAfter + text; lastClose = newText.lastIndexOf('>'); lastOpen = newText.lastIndexOf('<'); if (lastClose != -1 && lastOpen != -1 && lastOpen < lastClose) { return getQuery(textBefore, newText.substring(0, lastOpen)); } line++; } if (textBefore.contains("<")) { textBefore = textBefore.substring(textBefore.indexOf('<') + 1); } if (textAfter.contains(">")) { textAfter = textAfter.substring(0, textAfter.indexOf('>')); } return getQuery(textBefore, textAfter); } @Inject  AngularJSHtmlCodeAssistProcessor(AngularJSResources angularJSResources); @Override void computeCompletionProposals(TextEditor textEditor, int offset, CodeAssistCallback codeAssistCallback); @Override String getErrorMessage();  }
@Test public void testGenerateQueryPrefix() { AngularJSHtmlCodeAssistProcessor codeAssistProcessor = new AngularJSHtmlCodeAssistProcessor(null); AngularJSQuery query = codeAssistProcessor.getQuery("body", " "); assertEquals("body", query.getPrefix()); assertTrue(Arrays.equals(new String[]{}, query.getExistingAttributes().toArray())); query = codeAssistProcessor.getQuery("body ", "att1=\"val1\" att2=\"val2\""); assertEquals("", query.getPrefix()); assertTrue(Arrays.equals(new String[]{"body", "att1", "att2"}, query.getExistingAttributes().toArray())); query = codeAssistProcessor.getQuery("body att1", "att2=\"val2\""); assertEquals("att1", query.getPrefix()); assertTrue(Arrays.equals(new String[]{"body", "att2"}, query.getExistingAttributes().toArray())); }
@Test public void testInvalidXML() { AngularJSHtmlCodeAssistProcessor codeAssistProcessor = new AngularJSHtmlCodeAssistProcessor(null); TextEditor textEditor = Mockito.mock(TextEditor.class); EmbeddedDocument document = Mockito.mock(EmbeddedDocument.class); doReturn(document).when(textEditor).getDocument(); TextPosition cursorPosition = Mockito.mock(TextPosition.class); doReturn(cursorPosition).when(document).getCursorPosition(); document.getCursorPosition(); doReturn(0).when(cursorPosition).getCharacter(); doReturn(2).when(cursorPosition).getLine(); doReturn(8).when(document).getLineCount(); doReturn(" <script>").when(document).getLineContent(1); doReturn(" ").when(document).getLineContent(2); doReturn("function activate_section(section_header) {").when(document).getLineContent(3); doReturn(" ").when(document).getLineContent(4); doReturn("if (section_to_activate.className.indexOf(\"section-active\") >= 0) {").when(document).getLineContent(5); doReturn(" ").when(document).getLineContent(6); doReturn(" ").when(document).getLineContent(7); doReturn(" ").when(document).getLineContent(8); AngularJSQuery query = codeAssistProcessor.getQuery(textEditor); assertEquals("script>", query.getPrefix()); }