Skip to content

Commit

Permalink
fixed #117
Browse files Browse the repository at this point in the history
  • Loading branch information
ignatov committed May 3, 2013
1 parent e4b5666 commit d402618
Show file tree
Hide file tree
Showing 29 changed files with 1,249 additions and 375 deletions.
Expand Up @@ -45,10 +45,10 @@ static ElementDocProvider create(@NotNull PsiElement psiElement) {
}
final ErlangModule erlangModule = (ErlangModule) psiElement;
if (isFileFromErlangSdk(project, virtualFile)) {
return new SdkModuleDocProvider(project, virtualFile);
return new ErlangSdkModuleDocProvider(project, virtualFile);
}
else {
return new ModuleDocProvider(erlangModule);
return new ErlangModuleDocProvider(erlangModule);
}
}
else if (psiElement instanceof ErlangFunction) {
Expand All @@ -58,11 +58,11 @@ else if (psiElement instanceof ErlangFunction) {
}
final ErlangFunction erlangFunction = (ErlangFunction) psiElement;
if (isFileFromErlangSdk(project, virtualFile)) {
return new SdkFunctionDocProvider(project, erlangFunction.getName(),
return new ErlangSdkFunctionDocProvider(project, erlangFunction.getName(),
erlangFunction.getArity(), virtualFile);
}
else {
return new FunctionDocProvider(erlangFunction);
return new ErlangFunctionDocProvider(erlangFunction);
}
}
else if (psiElement instanceof ErlangTypeDefinition) {
Expand All @@ -72,7 +72,7 @@ else if (psiElement instanceof ErlangTypeDefinition) {
}
final ErlangTypeDefinition typeDefinition = (ErlangTypeDefinition) psiElement;
if (isFileFromErlangSdk(project, virtualFile)) {
return new SdkTypeDocProvider(project, virtualFile, typeDefinition.getName());
return new ErlangSdkTypeDocProvider(project, virtualFile, typeDefinition.getName());
}
else {
return null; // TODO implement TypeDocProvider
Expand All @@ -94,7 +94,7 @@ else if (psiElement instanceof ErlangTypeDefinition) {
if (tentativeErlangModule instanceof ErlangModule) {
final VirtualFile virtualFile = getVirtualFile(tentativeErlangModule);
if (virtualFile != null) {
return new SdkFunctionDocProvider(project, functionName, arity, virtualFile);
return new ErlangSdkFunctionDocProvider(project, functionName, arity, virtualFile);
}
}
}
Expand Down
35 changes: 31 additions & 4 deletions src/org/intellij/erlang/documentation/ErlangDocUtil.java
Expand Up @@ -16,12 +16,17 @@

package org.intellij.erlang.documentation;

import com.intellij.lang.ASTNode;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiComment;
import com.intellij.psi.PsiElement;
import com.intellij.psi.formatter.FormatterUtil;
import com.intellij.util.Function;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public final class ErlangDocUtil {
Expand Down Expand Up @@ -52,10 +57,15 @@ public final class ErlangDocUtil {
private ErlangDocUtil() {
}

static String getCommentText(@NotNull PsiComment comment,
@NotNull final String commentStartsWith,
@NotNull final Set<String> contextTags) {
String[] lines = StringUtil.splitByLines(comment.getText());
public static String getCommentsText(@NotNull List<PsiComment> comments,
@NotNull final String commentStartsWith,
@NotNull final Set<String> contextTags) {
List<String> lines = ContainerUtil.map(comments, new Function<PsiComment, String>() {
@Override
public String fun(PsiComment psiComment) {
return psiComment.getText();
}
});
return StringUtil.join(ContainerUtil.map(lines, new Function<String, String>() {
@Override
public String fun(String s) {
Expand All @@ -67,4 +77,21 @@ public String fun(String s) {
}
}), "<br/>");
}

@NotNull
static List<PsiComment> collectPrevComments(@NotNull PsiComment comment) {
ArrayList<PsiComment> result = new ArrayList<PsiComment>();
PsiElement current = comment;
while (current instanceof PsiComment) {
result.add((PsiComment) current);
ASTNode sibling = FormatterUtil.getPreviousNonWhitespaceSibling(current.getNode());
if (sibling != null) {
current = sibling.getPsi();
}
else {
current = null;
}
}
return ContainerUtil.reverse(result);
}
}
Expand Up @@ -27,10 +27,10 @@

import java.util.List;

final class FunctionDocProvider implements ElementDocProvider {
final class ErlangFunctionDocProvider implements ElementDocProvider {
@NotNull private final ErlangFunction myErlangFunction;

public FunctionDocProvider(@NotNull ErlangFunction erlangFunction) {
public ErlangFunctionDocProvider(@NotNull ErlangFunction erlangFunction) {
myErlangFunction = erlangFunction;
}

Expand All @@ -46,14 +46,15 @@ public String getDocText() {
final ErlangFunction prevFunction = PsiTreeUtil.getPrevSiblingOfType(myErlangFunction, ErlangFunction.class);
final ErlangSpecification spec = ErlangPsiImplUtil.getSpecification(myErlangFunction);
final PsiComment comment = PsiTreeUtil.getPrevSiblingOfType(myErlangFunction, PsiComment.class);

String commentText = "";
if (spec != null && ErlangPsiImplUtil.notFromPreviousFunction(spec, prevFunction)) {
commentText += spec.getText().replaceFirst("spec", "<b>Specification:</b><br/>") + "<br/><br/>";
}
if (comment != null && comment.getTokenType() == ErlangParserDefinition.ERL_FUNCTION_DOC_COMMENT &&
ErlangPsiImplUtil.notFromPreviousFunction(comment, prevFunction)) {
commentText += "<b>Comment:</b><br/>" + ErlangDocUtil.getCommentText(
comment, "%%", ErlangDocUtil.EDOC_FUNCTION_TAGS);
commentText += "<b>Comment:</b><br/>" + ErlangDocUtil.getCommentsText(
ErlangDocUtil.collectPrevComments(comment), "%%", ErlangDocUtil.EDOC_FUNCTION_TAGS);
}
return commentText;
}
Expand Down
Expand Up @@ -26,10 +26,10 @@

import java.util.List;

final class ModuleDocProvider implements ElementDocProvider {
final class ErlangModuleDocProvider implements ElementDocProvider {
@NotNull private final ErlangModule myErlangModule;

public ModuleDocProvider(@NotNull ErlangModule erlangModule) {
public ErlangModuleDocProvider(@NotNull ErlangModule erlangModule) {
myErlangModule = erlangModule;
}

Expand All @@ -45,7 +45,7 @@ public String getDocText() {
PsiElement parent = myErlangModule.getParent();
PsiComment comment = PsiTreeUtil.getPrevSiblingOfType(parent, PsiComment.class);
if (comment != null && comment.getTokenType() == ErlangParserDefinition.ERL_MODULE_DOC_COMMENT) {
return ErlangDocUtil.getCommentText(comment, "%%%", ErlangDocUtil.EDOC_MODULE_TAGS);
return ErlangDocUtil.getCommentsText(ErlangDocUtil.collectPrevComments(comment), "%%%", ErlangDocUtil.EDOC_MODULE_TAGS);
}
return null;
}
Expand Down
Expand Up @@ -44,7 +44,7 @@

import static com.intellij.codeInsight.documentation.DocumentationManager.PSI_ELEMENT_PROTOCOL;

abstract class AbstractSdkDocProvider implements ElementDocProvider {
abstract class ErlangSdkDocProviderBase implements ElementDocProvider {
private static final Pattern PATTERN_HREF = Pattern.compile("<a href=\"(.*?)\">");
private static final Pattern PATTERN_EVALUATED_LINK = Pattern.compile("javascript:erlhref\\('.*?','.*?','(.*?)'\\);");
private static final Pattern PATTERN_EXTERNAL_LINK = Pattern.compile("(.*)\\.html#(.*)");
Expand All @@ -54,7 +54,7 @@ abstract class AbstractSdkDocProvider implements ElementDocProvider {
final String css;
try {
css = ResourceUtil.loadText(ResourceUtil.getResource(
AbstractSdkDocProvider.class, "/documentation", "erlang-sdk-doc.css"));
ErlangSdkDocProviderBase.class, "/documentation", "erlang-sdk-doc.css"));
} catch (IOException e) {
throw (AssertionError) (new AssertionError().initCause(e));
}
Expand All @@ -66,7 +66,7 @@ abstract class AbstractSdkDocProvider implements ElementDocProvider {
@Nullable private List<OrderEntry> myOrderEntries;
@Nullable private List<String> myExternalDocUrls;

protected AbstractSdkDocProvider(@NotNull Project project, @NotNull VirtualFile virtualFile) {
protected ErlangSdkDocProviderBase(@NotNull Project project, @NotNull VirtualFile virtualFile) {
myProject = project;
myVirtualFile = virtualFile;
}
Expand Down
Expand Up @@ -23,7 +23,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

final class SdkFunctionDocProvider extends AbstractSdkDocProvider {
final class ErlangSdkFunctionDocProvider extends ErlangSdkDocProviderBase {
private static final Pattern PATTERN_FUNC_BEGIN = Pattern.compile(
"<a name=\"(.*?)\"></a><span class=\"bold_code\">.*?</span><br>");
private static final Pattern PATTERN_BIF_BEGIN = Pattern.compile(
Expand All @@ -32,8 +32,8 @@ final class SdkFunctionDocProvider extends AbstractSdkDocProvider {

@NotNull private final String myFuncSignature;

public SdkFunctionDocProvider(@NotNull Project project, @NotNull String functionName, int functionArity,
@NotNull VirtualFile virtualFile) {
public ErlangSdkFunctionDocProvider(@NotNull Project project, @NotNull String functionName, int functionArity,
@NotNull VirtualFile virtualFile) {
super(project, virtualFile);
myFuncSignature = functionName + "-" + functionArity;
}
Expand Down
Expand Up @@ -22,11 +22,11 @@

import java.util.regex.Pattern;

final class SdkModuleDocProvider extends AbstractSdkDocProvider {
final class ErlangSdkModuleDocProvider extends ErlangSdkDocProviderBase {
private static final Pattern PATTERN_MODULE_BEGIN = Pattern.compile("^ <h3>MODULE</h3>$");
private static final Pattern PATTERN_MODULE_END = Pattern.compile("^ <h3>EXPORTS</h3>$");

public SdkModuleDocProvider(@NotNull Project project, @NotNull VirtualFile virtualFile) {
public ErlangSdkModuleDocProvider(@NotNull Project project, @NotNull VirtualFile virtualFile) {
super(project, virtualFile);
}

Expand Down
Expand Up @@ -23,14 +23,14 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

final class SdkTypeDocProvider extends AbstractSdkDocProvider {
final class ErlangSdkTypeDocProvider extends ErlangSdkDocProviderBase {
private static final Pattern PATTERN_TYPE_BEGIN = Pattern.compile(
"^ <span class=\"bold_code\"><a name=\"type-(.*?)\">.*?</span><br></p>$");
private static final Pattern PATTERN_FUNC_BEGIN = Pattern.compile("^ <h3>EXPORTS</h3>$");

@NotNull private final String myTypeName;

public SdkTypeDocProvider(@NotNull Project project, @NotNull VirtualFile virtualFile, @NotNull String typeName) {
public ErlangSdkTypeDocProvider(@NotNull Project project, @NotNull VirtualFile virtualFile, @NotNull String typeName) {
super(project, virtualFile);
myTypeName = typeName;
}
Expand Down
8 changes: 7 additions & 1 deletion src/org/intellij/erlang/formatter/ErlangFormattingBlock.java
Expand Up @@ -36,6 +36,7 @@
import java.util.Collections;
import java.util.List;

import static org.intellij.erlang.ErlangParserDefinition.COMMENTS;
import static org.intellij.erlang.ErlangTypes.*;

/**
Expand Down Expand Up @@ -79,7 +80,7 @@ public ErlangFormattingBlock(@NotNull ASTNode node,
mySettings = settings;
myErlangSettings = erlangSettings;
mySpacingBuilder = spacingBuilder;
myIndent = new ErlangIndentProcessor(mySettings).getChildIndent(node);
myIndent = ErlangIndentProcessor.getChildIndent(node);
}

@Override
Expand Down Expand Up @@ -167,6 +168,11 @@ private AlignmentStrategy createOrGetAlignmentStrategy() {
@Override
@Nullable
public Spacing getSpacing(@Nullable Block child1, @NotNull Block child2) {
if (child2 instanceof ErlangFormattingBlock) {
if (COMMENTS.contains(((ErlangFormattingBlock) child2).getNode().getElementType()) && mySettings.KEEP_FIRST_COLUMN_COMMENT) {
return Spacing.createKeepingFirstColumnSpacing(0, Integer.MAX_VALUE, true, mySettings.KEEP_BLANK_LINES_IN_CODE);
}
}
return mySpacingBuilder.getSpacing(this, child1, child2);
}

Expand Down
12 changes: 1 addition & 11 deletions src/org/intellij/erlang/formatter/ErlangIndentProcessor.java
Expand Up @@ -18,7 +18,6 @@

import com.intellij.formatting.Indent;
import com.intellij.lang.ASTNode;
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
import com.intellij.psi.formatter.FormatterUtil;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.util.PsiTreeUtil;
Expand All @@ -30,7 +29,6 @@

import java.util.Set;

import static org.intellij.erlang.ErlangParserDefinition.COMMENTS;
import static org.intellij.erlang.ErlangTypes.*;

/**
Expand All @@ -44,13 +42,8 @@ public class ErlangIndentProcessor {
ERL_OP_LT, ERL_OP_EQ_LT, ERL_OP_GT, ERL_OP_GT_EQ, ERL_OP_LT_EQ, ERL_OP_PLUS_PLUS,
ERL_OP_MINUS_MINUS, ERL_OP_EQ, ERL_OP_EXL, ERL_OP_LT_MINUS, ERL_ANDALSO, ERL_ORELSE
);
private final CommonCodeStyleSettings settings;

public ErlangIndentProcessor(CommonCodeStyleSettings settings) {
this.settings = settings;
}

public Indent getChildIndent(ASTNode node) {
public static Indent getChildIndent(ASTNode node) {
IElementType elementType = node.getElementType();
ASTNode parent = node.getTreeParent();
IElementType parentType = parent != null ? parent.getElementType() : null;
Expand All @@ -62,9 +55,6 @@ public Indent getChildIndent(ASTNode node) {
if (parent == null || parent.getTreeParent() == null) {
return Indent.getNoneIndent();
}
if (COMMENTS.contains(elementType) && settings.KEEP_FIRST_COLUMN_COMMENT) {
return Indent.getAbsoluteNoneIndent();
}

if (elementType == ERL_CATCH) {
return Indent.getNoneIndent();
Expand Down
4 changes: 2 additions & 2 deletions src/org/intellij/erlang/parser/Erlang.flex
Expand Up @@ -35,8 +35,8 @@ ModuleDocCommentLine = "%%%"[^\r\n]*
FunctionDocCommentLine = "%%"[^\r\n]*
CommentLine = "%"[^\r\n]*

ModuleDocComment = {ModuleDocCommentLine} ({Whitespace}? {ModuleDocCommentLine})*
FunctionDocComment = {FunctionDocCommentLine} ({Whitespace}? {FunctionDocCommentLine})*
ModuleDocComment = {ModuleDocCommentLine} {ModuleDocCommentLine}*
FunctionDocComment = {FunctionDocCommentLine} {FunctionDocCommentLine}*
Comment = {CommentLine} {CommentLine}*
Whitespace = ([ \t\n] | {ControlCharacter})+
ErlangUppercase = [A-Z]
Expand Down

0 comments on commit d402618

Please sign in to comment.