Skip to content

Commit

Permalink
Fixed issues identified in code review
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan J.A. Murphy committed Oct 16, 2021
1 parent 1cb821a commit 83a69bc
Showing 1 changed file with 90 additions and 86 deletions.
176 changes: 90 additions & 86 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export default class LumberjackPlugin extends Plugin {
const parameters = 𖣂 as unknown as Parameters;

for (const parameter in parameters) { // not yet using parameters
(parameters as any)[parameter] = decodeURIComponent((parameters as any)[parameter]); // Thanks to @Vinzent03 for a clear-cut (pun intended) example of how to do this
(parameters as any)[parameter] = (parameters as any)[parameter]; // Thanks to @Vinzent03 for a clear-cut (pun intended) example of how to do this
}

this.newLog(this.settings.alwaysOpenInNewLeaf);
Expand All @@ -68,8 +68,8 @@ export default class LumberjackPlugin extends Plugin {

const parameters = 𖣂 as unknown as Parameters;

for (const parameter in parameters) {
(parameters as any)[parameter] = decodeURIComponent((parameters as any)[parameter]); // Thanks to @Vinzent03 for a clear-cut (pun intended) example of how to do this
for (const parameter in parameters) { // not yet actually using parameters
(parameters as any)[parameter] = (parameters as any)[parameter]; // Thanks to @Vinzent03 for a clear-cut (pun intended) example of how to do this
}

if (parameters.name) {
Expand Down Expand Up @@ -98,91 +98,94 @@ export default class LumberjackPlugin extends Plugin {

// open the daily note in edit mode and get the editor
let openedDailyNote = await this.app.workspace.openLinkText(dailyNote.name, dailyNote.path, openFileInNewLeaf, editModeState);
let editor = this.app.workspace.getActiveViewOfType(MarkdownView).editor;
if (editor == null) {
new Notice(`Could not find the daily note. Check your daily note settings, or report this as a bug on the plugin repository.`);
return
}
if (this.app.workspace.getActiveViewOfType(MarkdownView)) {
let editor = this.app.workspace.getActiveViewOfType(MarkdownView).editor;
if (editor == null) {
new Notice(`Could not find the daily note. Check your daily note settings, or report this as a bug on the plugin repository.`);
return
}

// establish the line prefix to add, if anything
let linePrefix = `
${this.settings.logPrefix}${tampTime}`

// Make sure the editor has focus
editor.focus();
// establish the line prefix to add, if anything
let linePrefix = `
${this.settings.logPrefix}${tampTime}`

// Inserting the cursor
// The goal is to set the cursor either at the end of the user's target section, if set, or at the end of the note
// find the section to insert the log item into and set the insert position to append into it, or set the insert position to the end of the note
let sections = this.app.metadataCache.getFileCache(dailyNote).headings;
if (this.settings.targetHeader != "") { // If the user has set a target header
// need to figure out which line the _next_ section is on, if any, then use that line number in the functions below
let targetSection = sections.find( (eachSection) => (eachSection.heading === this.settings.targetHeader)); // does the heading we're looking for exist?
if (typeof(targetSection !== undefined)) { // The target section exists
let nextSection = sections.find( (eachSection) => ((eachSection.position.start.line > targetSection.position.start.line) && (eachSection.level <= targetSection.level))); // matches sections _after_ our target, with the same level or greater
console.debug(nextSection);
if (!nextSection) {
// There is no section following the target section. Look for the end of the document
// A better approach would append the item at the end of the content inside the user's target section, because it's possible that someone would put more stuff in their daily note without a following section, but that will have to be implemented later.
console.debug("No section follows the target section. Inserting the log item at the end of the target section.")
editor.setCursor(editor.lastLine());
editor.replaceSelection(linePrefix);
editor.setCursor(editor.lastLine());
} else {
if (typeof(nextSection) !== undefined) { // The search for a following section did not return undefined, therefore it exists
// Find out if there is a preceding blank line before the next section. E.g., does the user use linebreaks to separate content in edit mode? If so, inserting the next item after that line break will look messy.
if (editor.getLine(nextSection.position.start.line - 1).length > 0) {
// The line just before the next section header is not blank. Insert the log item just before the next section, without a line break.
console.debug(`The line before the next section header is ${editor.getLine(nextSection.position.start.line - 1).toString()}`);
console.debug("No blank lines found between the target section and the next section.");
editor.setCursor(nextSection.position.start.line - 1);
editor.replaceSelection(linePrefix);
editor.setCursor(nextSection.position.start.line);
} else {
console.debug(`The line before the next section has 0 length. It is: ${editor.getLine(nextSection.position.start.line - 1)}`)
// The line just before the next section header is blank. It's likely that the user uses line breaks to clean up their note in edit mode.
// The approach here is to iterate over the lines preceding the next section header until a non-blank line is reached, then insert the log item at (iterator.position.start.line + 1)...
let lastBlankLineFound = false;
let noBlankLines = false;
let lastLineBeforeLineBreakIteratorLineNumber = nextSection.position.start.line - 2; // `lastLineBeforeLineBreakIteratorNumber: this wordy variable represents the number of the last-line-before-line-break iterator's current line
while (lastBlankLineFound == false) {
let blankLineFinderCurrentLine = editor.getLine(lastLineBeforeLineBreakIteratorLineNumber);
if (editor.getLine(0) === blankLineFinderCurrentLine) { // This condition would mean the iterator found the start of the document
noBlankLines = true;
lastBlankLineFound = false;
} else {
if (blankLineFinderCurrentLine.length > 0) {
lastBlankLineFound = true;
// Make sure the editor has focus
editor.focus();

// Inserting the cursor
// The goal is to set the cursor either at the end of the user's target section, if set, or at the end of the note
// find the section to insert the log item into and set the insert position to append into it, or set the insert position to the end of the note
let sections = this.app.metadataCache.getFileCache(dailyNote).headings;
if (this.settings.targetHeader != "") { // If the user has set a target header
// need to figure out which line the _next_ section is on, if any, then use that line number in the functions below
let targetSection = sections.find( (eachSection) => (eachSection.heading === this.settings.targetHeader)); // does the heading we're looking for exist?
if (typeof(targetSection !== undefined)) { // The target section exists
let nextSection = sections.find( (eachSection) => ((eachSection.position.start.line > targetSection.position.start.line) && (eachSection.level <= targetSection.level))); // matches sections _after_ our target, with the same level or greater
console.debug(nextSection);
if (!nextSection) {
// There is no section following the target section. Look for the end of the document
// A better approach would append the item at the end of the content inside the user's target section, because it's possible that someone would put more stuff in their daily note without a following section, but that will have to be implemented later.
console.debug("No section follows the target section. Inserting the log item at the end of the target section.")
editor.setCursor(editor.lastLine());
editor.replaceSelection(linePrefix);
editor.setCursor(editor.lastLine());
} else {
if (typeof(nextSection) !== undefined) { // The search for a following section did not return undefined, therefore it exists
// Find out if there is a preceding blank line before the next section. E.g., does the user use linebreaks to separate content in edit mode? If so, inserting the next item after that line break will look messy.

if (editor.getLine(nextSection.position.start.line - 1).length > 0) {
// The line just before the next section header is not blank. Insert the log item just before the next section, without a line break.
console.debug(`The line before the next section header is ${editor.getLine(nextSection.position.start.line - 1).toString()}`);
console.debug("No blank lines found between the target section and the next section.");
editor.setCursor(nextSection.position.start.line - 1);
editor.replaceSelection(linePrefix);
editor.setCursor(nextSection.position.start.line);
} else {
console.debug(`The line before the next section has 0 length. It is: ${editor.getLine(nextSection.position.start.line - 1)}`)
// The line just before the next section header is blank. It's likely that the user uses line breaks to clean up their note in edit mode.
// The approach here is to iterate over the lines preceding the next section header until a non-blank line is reached, then insert the log item at (iterator.position.start.line + 1)...
let lastBlankLineFound = false;
let noBlankLines = false;
let lastLineBeforeLineBreakIteratorLineNumber = nextSection.position.start.line - 2; // `lastLineBeforeLineBreakIteratorNumber: this wordy variable represents the number of the last-line-before-line-break iterator's current line
while (lastBlankLineFound == false) {
let blankLineFinderCurrentLine = editor.getLine(lastLineBeforeLineBreakIteratorLineNumber);
if (editor.getLine(0) === blankLineFinderCurrentLine) { // This condition would mean the iterator found the start of the document
noBlankLines = true;
lastBlankLineFound = false;
} else {
lastLineBeforeLineBreakIteratorLineNumber = lastLineBeforeLineBreakIteratorLineNumber - 1; // Move to the next line up
if (blankLineFinderCurrentLine.length > 0) {
lastBlankLineFound = true;
} else {
lastLineBeforeLineBreakIteratorLineNumber = lastLineBeforeLineBreakIteratorLineNumber - 1; // Move to the next line up
}
}
}
}

if (noBlankLines) { // this means the iterator failed to find any blanks at all; insert the log item just before the next section.
console.debug("No blank lines found.");
editor.setCursor(nextSection.position.start.line - 1);
editor.replaceSelection(linePrefix);
editor.setCursor(nextSection.position.start.line - 1);
} else { // There were an arbitrary number of blank lines before the next section header. Insert the log item _after_ the last (length > 0) line before the next section header.
console.debug(`Iterator stopped at line ${lastLineBeforeLineBreakIteratorLineNumber}, with text ${editor.getLine(lastLineBeforeLineBreakIteratorLineNumber)}`);
editor.setCursor(lastLineBeforeLineBreakIteratorLineNumber);
editor.replaceSelection(linePrefix);
editor.setCursor(lastLineBeforeLineBreakIteratorLineNumber + 1);
}
if (noBlankLines) { // this means the iterator failed to find any blanks at all; insert the log item just before the next section.
console.debug("No blank lines found.");
editor.setCursor(nextSection.position.start.line - 1);
editor.replaceSelection(linePrefix);
editor.setCursor(nextSection.position.start.line - 1);
} else { // There were an arbitrary number of blank lines before the next section header. Insert the log item _after_ the last (length > 0) line before the next section header.
console.debug(`Iterator stopped at line ${lastLineBeforeLineBreakIteratorLineNumber}, with text ${editor.getLine(lastLineBeforeLineBreakIteratorLineNumber)}`);
editor.setCursor(lastLineBeforeLineBreakIteratorLineNumber);
editor.replaceSelection(linePrefix);
editor.setCursor(lastLineBeforeLineBreakIteratorLineNumber + 1);
}



}
}
}
}
} else {
// The user has not set a target header. Insert the log item at the bottom of the note.
editor.setCursor(editor.lastLine());
editor.replaceSelection(linePrefix);
editor.setCursor(editor.lastLine());
}
} else {
// The user has not set a target header. Insert the log item at the bottom of the note.
editor.setCursor(editor.lastLine());
editor.replaceSelection(linePrefix);
editor.setCursor(editor.lastLine());
}


Expand Down Expand Up @@ -223,19 +226,20 @@ ${this.settings.logPrefix}${tampTime}`

let newDraft = await this.app.workspace.openLinkText(zkUUIDNoteName, `/${this.settings.inboxFilePath}/`, this.settings.alwaysOpenInNewLeaf, editModeState);

let editor = this.app.workspace.getActiveViewOfType(MarkdownView).editor;
if (this.app.workspace.getActiveViewOfType(MarkdownView)) {
let editor = this.app.workspace.getActiveViewOfType(MarkdownView).editor;
if (editor == null) {
new Notice(`Couldn't run the Timber command. Please report this as a bug in the GitHub repository for Lumberjack 🪓🪵`);
return
}

if (editor == null) {
new Notice(`Could not find the daily note. Check your daily note settings, or report this as a bug on the plugin repository.`);
return
editor.focus();
let startChar = "";
editor.setCursor(editor.lastLine());
editor.replaceSelection(startChar);
editor.setCursor(editor.lastLine());
}

editor.focus();
let startChar = "";
editor.setCursor(editor.lastLine());
editor.replaceSelection(startChar);
editor.setCursor(editor.lastLine());

}

onunload() {
Expand Down

0 comments on commit 83a69bc

Please sign in to comment.