Skip to content

Commit

Permalink
Merge pull request #3 from moacirrf/fix_fix_fonts_styles
Browse files Browse the repository at this point in the history
Fix fix fonts styles
  • Loading branch information
moacirrf authored Sep 16, 2022
2 parents 4041408 + 5a93cc5 commit 4728877
Show file tree
Hide file tree
Showing 23 changed files with 248 additions and 182 deletions.
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
Markdown for Netbeans ![master](https://github.com/moacirrf/netbeans-markdown/actions/workflows/maven-publish.yml/badge.svg)
=====================================
# Markdown for Netbeans ![master](https://github.com/moacirrf/netbeans-markdown/actions/workflows/maven-publish.yml/badge.svg)

***

## Description
This plugin include some aditionals features to Apache Netbeans Markdown Editor.
This plugin include some additionals features to Apache Netbeans Markdown Editor.
- Preview
- Split Window
- Autocomplete
- Sugestion

### Problems
- A more accuracy syncronized scroll

## Requirements
- Apache Netbeans 14, 15
- JDK 11+

Expand All @@ -20,7 +19,9 @@ This plugin include some aditionals features to Apache Netbeans Markdown Editor.
- Flexmark - BSD 2-Clause "Simplified" License
- Apache Batik - Apache License, Version 2.0
- Jsoup - The MIT License
- Apache Commons Text - Apache License, Version 2.0


## Screenshots

![image](https://user-images.githubusercontent.com/950706/190041477-71d5b5fc-d887-4934-810a-0ceb1048c607.png)
## Screenshots
<img src="https://user-images.githubusercontent.com/950706/190041477-71d5b5fc-d887-4934-810a-0ceb1048c607.png" width="1700" />
25 changes: 23 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.moacirrf</groupId>
<artifactId>netbeans-markdown</artifactId>
<version>0.0.1</version>
<version>0.0.2</version>
<packaging>nbm</packaging>
<name>Netbeans Markdown</name>
<url>https://github.com/moacirrf/netbeans-markdown</url>
Expand All @@ -22,7 +22,7 @@
<connection>scm:git:https://github.com/moacirrf/netbeans-markdown</connection>
<developerConnection>scm:git:https://github.com/moacirrf/netbeans-markdown</developerConnection>
<url>https://github.com/moacirrf/netbeans-markdown/tree/${project.scm.tag}</url>
<tag>v0.0.1</tag>
<tag>v0.0.2</tag>
</scm>

<licenses>
Expand Down Expand Up @@ -160,6 +160,12 @@
<version>1.14</version>
</dependency>

<!-- Commons Text-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.9</version>
</dependency>

<!-- Junit -->
<dependency>
Expand Down Expand Up @@ -245,6 +251,21 @@
<version>RELEASE140</version>
<type>jar</type>
</dependency>

<dependency>
<groupId>org.netbeans.api</groupId>
<artifactId>org-netbeans-modules-editor-mimelookup</artifactId>
<version>RELEASE140</version>
<type>jar</type>
</dependency>

<dependency>
<groupId>org.netbeans.api</groupId>
<artifactId>org-netbeans-modules-editor-settings</artifactId>
<version>RELEASE140</version>
<type>jar</type>
</dependency>

</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/
public final class Icons {

private static final String PATH = "/io/github/moacirrf/netbeans/markdown/";
private static final String PATH = "io/github/moacirrf/netbeans/markdown/";

public static ImageIcon getICON_PREVIEW() {
return loadImageIcon(PATH + "icon_preview.png", true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* @author Moacir da Roza Flores <[email protected]>
*/
public class Installer extends ModuleInstall {

@Override
public boolean closing() {
TempDir.removeTempDir();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@
@DataObject.Registration(
mimeType = MarkdownDataObject.MIME_TYPE,
iconBase = "io/github/moacirrf/netbeans/markdown/markdown.png",
displayName = "#LBL_Markdown_LOADER",
position = 300
displayName = "#LBL_Markdown_LOADER"
)
@ActionReferences({
@ActionReference(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.util.Objects;
import javax.swing.ImageIcon;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
Expand All @@ -40,7 +41,6 @@ public abstract class AbstractCompletionItem implements CompletionItem {
private String leftHtmlText;
private String righHtmlText;
private String template;

private int startOffset;
private int sortPriority;

Expand Down Expand Up @@ -154,4 +154,30 @@ public void setSortPriority(int sortPriority) {
this.sortPriority = sortPriority;
}

@Override
public int hashCode() {
int hash = 7;
hash = 11 * hash + Objects.hashCode(this.leftHtmlText);
hash = 11 * hash + Objects.hashCode(this.righHtmlText);
return hash;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final AbstractCompletionItem other = (AbstractCompletionItem) obj;
if (!Objects.equals(this.leftHtmlText, other.leftHtmlText)) {
return false;
}
return Objects.equals(this.righHtmlText, other.righHtmlText);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public static AbstractCompletionItem newItem(
String leftText,
String rightText,
int startOffset) {

var item = new CompletionItemImpl();
item.setIcon(icon);
item.setLeftHtmlText(leftText);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
import static io.github.moacirrf.netbeans.markdown.completion.CompletionItemImpl.newItem;
import static io.github.moacirrf.netbeans.markdown.Icons.getICON_COMPLETION;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import javax.swing.text.Document;
import org.netbeans.spi.editor.completion.CompletionItem;
import org.netbeans.spi.editor.completion.CompletionResultSet;
import org.netbeans.spi.editor.completion.support.AsyncCompletionQuery;
import org.openide.util.Exceptions;
Expand All @@ -33,21 +33,23 @@
*/
public class CompletionQuery extends AsyncCompletionQuery {

private final Set<AbstractCompletionItem> itens = new HashSet<>();

@Override
protected void query(CompletionResultSet crs, Document document, int caretOffset) {
crs.addAllItems(getFormatHints(document, caretOffset));
crs.addAllItems(getHeadings(document, caretOffset));
crs.addAllItems(getBlockquotes(document, caretOffset));
crs.addAllItems(getLists(document, caretOffset));
crs.addAllItems(getCode(document, caretOffset));
crs.addAllItems(getLinks(document, caretOffset));
crs.addAllItems(getImages(document, caretOffset));
crs.addAllItems(getTables(document, caretOffset));
getFormatHints(caretOffset);
getHeadings(caretOffset);
getBlockquotes(caretOffset);
getLists(caretOffset);
getCode(caretOffset);
getLinks(caretOffset);
getImages(caretOffset);
getTables(caretOffset);
crs.addAllItems(itens);
crs.finish();
}

private Collection<? extends CompletionItem> getFormatHints(Document document, int caretOffset) {
var itens = new ArrayList<CompletionItem>();
private Collection<? extends AbstractCompletionItem> getFormatHints(int caretOffset) {
var icon = getICON_COMPLETION();
itens.add(newItem(0, icon, "<i>Italic 1</i>", "*Italic 1*", caretOffset));
itens.add(newItem(0, icon, "<i>Italic 2</i>", "_Italic 2_", caretOffset));
Expand All @@ -61,8 +63,7 @@ private Collection<? extends CompletionItem> getFormatHints(Document document, i
return itens;
}

private Collection<? extends CompletionItem> getHeadings(Document document, int caretOffset) {
var itens = new ArrayList<AbstractCompletionItem>();
private Collection<? extends AbstractCompletionItem> getHeadings(int caretOffset) {
var icon = getICON_COMPLETION();
itens.add(newItem(2, icon, "<h1>Heading level 1</h1>", "# Heading level 1", caretOffset));
itens.add(newItem(2, icon, "<h2>Heading level 2</h2>", "## Heading level 2", caretOffset));
Expand All @@ -74,42 +75,37 @@ private Collection<? extends CompletionItem> getHeadings(Document document, int
return itens;
}

private Collection<? extends CompletionItem> getBlockquotes(Document document, int caretOffset) {
var itens = new ArrayList<CompletionItem>();
private Collection<? extends AbstractCompletionItem> getBlockquotes(int caretOffset) {
var icon = getICON_COMPLETION();
itens.add(newItem(3, icon, "Blockquote", "> Blockquote", caretOffset));

return itens;
}

private Collection<? extends CompletionItem> getLists(Document document, int caretOffset) {
var itens = new ArrayList<CompletionItem>();
private Collection<? extends AbstractCompletionItem> getLists(int caretOffset) {
var icon = getICON_COMPLETION();
itens.add(newItem(4, icon, "Ordered List", "1. Item 1\n", caretOffset));
itens.add(newItem(4, icon, "Unordered List", "- Item 1\n", caretOffset));

return itens;
}

private Collection<? extends CompletionItem> getCode(Document document, int caretOffset) {
var itens = new ArrayList<CompletionItem>();
private Collection<? extends AbstractCompletionItem> getCode(int caretOffset) {
var icon = getICON_COMPLETION();
itens.add(newItem(5, icon, "Code Block", " fun code(){}", caretOffset));

return itens;
}

private Collection<? extends CompletionItem> getLinks(Document document, int caretOffset) {
var itens = new ArrayList<CompletionItem>();
private Collection<? extends AbstractCompletionItem> getLinks(int caretOffset) {
var icon = getICON_COMPLETION();
itens.add(newItem(6, icon, "Link", "[Description](https://netbeans.apache.org/).", caretOffset));
itens.add(newItem(6, icon, "Email URL", "<https://netbeans.apache.org/)>\n"
+ "<[email protected]>", caretOffset));
return itens;
}

private Collection<? extends CompletionItem> getImages(Document document, int caretOffset) {
var itens = new ArrayList<CompletionItem>();
private Collection<? extends AbstractCompletionItem> getImages(int caretOffset) {
var icon = getICON_COMPLETION();
itens.add(newItem(7, icon, "Image 1",
"![Best IDE](https://netbeans.apache.org/images/nblogo48x48.png)", caretOffset));
Expand All @@ -118,11 +114,10 @@ private Collection<? extends CompletionItem> getImages(Document document, int ca
return itens;
}

private Collection<? extends CompletionItem> getTables(Document document, int caretOffset) {
var itens = new ArrayList<CompletionItem>();
private Collection<? extends AbstractCompletionItem> getTables(int caretOffset) {
var icon = getICON_COMPLETION();
try ( var stream = getClass().getResourceAsStream("/io/github/moacirrf/netbeans/markdown/completion/table.md")) {
var item = newItem(7, icon, "Table", "A three Column table", caretOffset);
var item = newItem(7, icon, "Table", "A Three Column Table", caretOffset);
item.setTemplate(new String(stream.readAllBytes()));
itens.add(item);
} catch (IOException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@ public class FirstElementAdjuster implements HtmlAdjuster {

@Override
public Document adjust(Document inputDocument) {
inputDocument.getElementsByTag("body").first().firstElementChild()
.addClass("removeMarginPaddingTop");
if (inputDocument != null) {
var body = inputDocument.getElementsByTag("body");
if (body != null && body.first() != null && body.first().firstElementChild() != null) {
body.first().firstElementChild()
.addClass("removeMarginPaddingTop");
}
}
return inputDocument;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,13 @@ public String build(String markdownText) {
var renderer = HtmlRenderer.builder(options).build();

Node document = parser.parse(markdownText);
if (document == null) {
return "";
}
Document doc = Jsoup.parse(renderer.render(document));

if (doc == null) {
return "";
}
if (!htmlAdjusters.isEmpty()) {
for (var adjuster : htmlAdjusters) {
doc = adjuster.adjust(doc);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ public class LinksAdjuster implements HtmlAdjuster {

@Override
public Document adjust(Document inputDocument) {
fixLinksWithImage(inputDocument.getElementsByTag("a"));
if (inputDocument != null) {
fixLinksWithImage(inputDocument.getElementsByTag("a"));
}
return inputDocument;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ public class TablesAdjuster implements HtmlAdjuster {

@Override
public Document adjust(Document document) {

document.getElementsByTag("table").forEach(table
-> fixTableRowsStyle(table.getElementsByTag("tr"))
);
if (document != null && document.getElementsByTag("table") != null) {
document.getElementsByTag("table").forEach(table
-> fixTableRowsStyle(table.getElementsByTag("tr"))
);
}
return document;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ public void fileChanged(FileEvent fe) {
super.fileChanged(fe);
syncronizeScrolls();
}

});
rightJScrollPane.setFileObject(mdFile);

Expand All @@ -106,17 +105,17 @@ private void syncronizeScrolls() {
}

private void setScrollToMaximum(JScrollPane scrollPane) {
Point p = scrollPane.getViewport().getViewPosition();
p.y = Integer.MAX_VALUE;
scrollPane.getViewport().setViewPosition(p);
var viewPort = scrollPane.getViewport();
var viewPosition = viewPort.getViewPosition();
var viewSize = viewPort.getViewSize();

viewPosition.y = viewSize.height;
scrollPane.getViewport().setViewPosition(viewPosition);
}

private boolean isScrolledToMaximum(JScrollPane scrollPane) {
var viewPort = scrollPane.getViewport();
if ((viewPort.getViewSize().height - viewPort.getExtentSize().getHeight()) == viewPort.getViewPosition().y) {
return true;
}
return false;
return (viewPort.getViewSize().height - viewPort.getExtentSize().getHeight()) == viewPort.getViewPosition().y;
}


Expand Down
Loading

0 comments on commit 4728877

Please sign in to comment.