Skip to content

Commit

Permalink
when jumping to variable source, take inheritance into account
Browse files Browse the repository at this point in the history
  • Loading branch information
Milos Kleint committed Mar 21, 2015
1 parent 42e17fe commit 71fc1b3
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 6 deletions.
9 changes: 8 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,18 @@
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.14</version>
</dependency>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<sourceDirectory>src</sourceDirectory>
<testSourceDirectory>test</testSourceDirectory>
<resources>
<resource>
<directory>src</directory>
Expand Down
27 changes: 22 additions & 5 deletions src/com/tropyx/nb_puppet/hyperlink/PHyperlinkProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ else if (token.id() == PTokenId.DEFINE || token.id() == PTokenId.CLASS) {
} else if (token.id() == PTokenId.VARIABLE) {
fTokenOff[0] = xml.offset();
fValue[0] = token.text().toString();
if (fValue[0].indexOf("::") != fValue[0].lastIndexOf("::") //heuristics, want XXX::YYY::variable name only
if (fValue[0].contains("::")
&& !fValue[0].startsWith("$::")) {
fAssociatedID[0] = token.id();
}
Expand All @@ -198,12 +198,15 @@ else if (token.id() == PTokenId.DEFINE || token.id() == PTokenId.CLASS) {
}

private boolean matchChains(DecisionNode node, TokenSequence<PTokenId> xml, boolean moveBack) {
return matchChainsRes(node, xml, moveBack) != null;
}
private Token<PTokenId> matchChainsRes(DecisionNode node, TokenSequence<PTokenId> xml, boolean moveBack) {
boolean doOneMore;
do {
doOneMore = false;
boolean didMove = moveBack ? xml.movePrevious() : xml.moveNext();
if (!didMove) {
return false;
return null;
}
Token<PTokenId> token = xml.token();
if (token.id() == PTokenId.WHITESPACE) {
Expand All @@ -214,7 +217,7 @@ private boolean matchChains(DecisionNode node, TokenSequence<PTokenId> xml, bool
if (ch.id.equals(token.id())) {
if (ch.children.length == 0) {
//we are at the end.
return true;
return token;
} else {
node = ch;
doOneMore = true;
Expand All @@ -227,7 +230,7 @@ private boolean matchChains(DecisionNode node, TokenSequence<PTokenId> xml, bool
}
// template ('sss/ss.rbm')
} while (doOneMore);
return false;
return null;
}

private DecisionNode createStringChains() {
Expand Down Expand Up @@ -301,6 +304,7 @@ private void performJump(Tuple tup, Document doc) {
private void cursorToVariableDefinition(DataObject dobj, String variableName) throws IndexOutOfBoundsException {
EditorCookie editc = dobj.getLookup().lookup(EditorCookie.class);
final int[] foffset = new int[1];
final String[] fInherit = new String[1];
foffset[0] = -1;
BaseDocument bd = null;
try {
Expand All @@ -320,6 +324,12 @@ public void run() {
if (token == null) {
return;
}
if (fInherit[0] == null && token.id() == PTokenId.CLASS) {
Token<PTokenId> tk = matchChainsRes(of(PTokenId.CLASS, of(PTokenId.IDENTIFIER, of(PTokenId.INHERITS, of(PTokenId.IDENTIFIER)))), xml, false);
if (tk != null) {
fInherit[0] = tk.text().toString();
}
}
if (token.id() == PTokenId.VARIABLE) {
if (fVariableName.equals(token.text().toString())) {
foffset[0] = token.offset(th);
Expand All @@ -333,6 +343,10 @@ public void run() {
int row = Utilities.getRowIndent(bd, foffset[0]);
LineCookie lc = dobj.getLookup().lookup(LineCookie.class);
lc.getLineSet().getOriginal(line).show(Line.ShowOpenType.REUSE, Line.ShowVisibilityType.FOCUS, row);
} else {
if (fInherit[0] != null) {
performJump(new Tuple("$" + fInherit[0] + "::" + variableName.replace("$", ""), PTokenId.VARIABLE, -1, null), bd);
}
}
} catch (IOException | BadLocationException ex) {
Exceptions.printStackTrace(ex);
Expand Down Expand Up @@ -400,12 +414,15 @@ private FileObject findFile(FileObject fo, String path) {
Pair<String, String> getPathAndVariable(String path) {
path = path.replace("$", "").replace("{", "").replace("}", "");
String[] splitValue = path.split("\\:\\:");
if (splitValue.length > 2) {
if (splitValue.length > 1) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < splitValue.length - 1; i++) {
sb.append(splitValue[i]);
if (i == 0) {
sb.append("/manifests/");
if (splitValue.length == 2) {
sb.append("init.pp");
}
} else if (i == splitValue.length - 2) {
sb.append(".pp");
} else {
Expand Down
50 changes: 50 additions & 0 deletions test/com/tropyx/nb_puppet/hyperlink/PHyperlinkProviderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (C) 2015 mkleint
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.tropyx.nb_puppet.hyperlink;

import org.junit.Test;
import static org.junit.Assert.*;
import org.openide.util.Pair;

/**
*
* @author mkleint
*/
public class PHyperlinkProviderTest {

public PHyperlinkProviderTest() {
}


@Test
public void testGetPathAndVariable() {
PHyperlinkProvider inst = new PHyperlinkProvider();
Pair<String, String> tup = inst.getPathAndVariable("mod::params::var");
assertEquals("mod/manifests/params.pp", tup.first());
assertEquals("$var", tup.second());

tup = inst.getPathAndVariable("mod::foo::params::var");
assertEquals("mod/manifests/foo/params.pp", tup.first());
assertEquals("$var", tup.second());

tup = inst.getPathAndVariable("mod::var");
assertEquals("mod/manifests/init.pp", tup.first());
assertEquals("$var", tup.second());
}


}

0 comments on commit 71fc1b3

Please sign in to comment.