sieukrem / jn-npp-plugin

Plugin for Notepad++ allowing you to automate some tasks using JavaScript
https://github.com/sieukrem/jn-npp-plugin/wiki
110 stars 24 forks source link

Refactoring #93

Closed WinterSilence closed 2 years ago

WinterSilence commented 4 years ago
  1. I found easy solution to emulate console.log() - JSON.stringify(var); , maybe with codemirror and display result in bottom dock, as example:
    class Console {
    constructor(config) {
        this.id = 'jn-console';
        // only for examlple - need rewrite Dialog.js
        let defaultConfig = {
            dockable: true,
            // css: 'body {overflow:scroll;background:#fff;color:#000;font-size:90%;font-family:Menlo,monospace;}',
            html: '<ol id="' + this.id + '"></ol>',
            name: 'JN console',
            docking: 'bottom'
        };
        this.dock = new Dialog(Object.assign(defaultConfig, config));
    }
    show() {
        this.dock.visible = true;
        // call by chain
        return this;
    }
    hide() {
        this.dock.visible = false;
        return this;
    }
    isVisible: function () {
        return this.dock && this.dock.visible;
    }
    writeLn(str) {
        str = '<li>' + (typeof str == 'String' ? str : JSON.stringify(str)) + '</li>';
        this.dock.document.getElementById(this.id).insertAdjacentHTML('beforeend', str);
    }
    clear{
        this.dock.document.getElementById(this.id).innerHTML = '';
    }
    dump(...data) {
        // @todo highlight + code folding https://codemirror.net/demo/folding.html
        return JSON.stringify(data.length == 1 ? data[0] : data);
    }
    /**
     * @param {string} command Path to CLI application or command.
     * @param {object} longArgs An array of long option/value pairs (without prefix `--`).
     * @param {object} shortArgs An array of short option/value pairs (without prefix `-`).
     */
    execute(command, longArgs = {}, shortArgs = {}) {
        Object.entries(longArgs).forEach(function (opt, value) {
            command += ' --' + opt + ' ' + JSON.stringify(value);
        });
        Object.entries(shortArgs).forEach(function (opt, value) {
            command += ' -' + opt + ' ' + JSON.stringify(value);
        });
        try {
            if (! this.shell) {
                this.shell = new ActiveXObject('WScript.Shell');
            }
            let scriptExec = this.shell.Exec(command);
            let error = scriptExec.StdErr.ReadAll();
            if (error) {
                throw new EvalError(error);
            }
            // append '' for fix wrong type at empty result
            return scriptExec.StdOut.ReadAll() + '';
        } catch (e) {
            this.writeLn('<mark>' + this.dump(e) + '</mark>');
            return false;
        }
    }
    }

2, FS class wrap FileSystemObject methods

debug was useful at the beginning as there was no VS Debugger Support

VS Debugger can explore dll content?!

WinterSilence commented 4 years ago
  1. need require lodash or something like this - after php write on js so hard - empty box (:
sieukrem commented 4 years ago

Good start 😄 Precise the Title and Description of the issue.

Im am sure there are much better tutorials "how to write perfect issue?" on the internet (e.g. Writing-Good-Feature-Requests).

sieukrem commented 4 years ago

VS Debugger can explore dll content?!

What do you mean? I meant that you can debug your javascript running in Npp by attaching to it from Visual Studio.

WinterSilence commented 4 years ago

Precise the Title and Description of the issue

it's common discuss & small project

What do you mean

I mean, debugger can inspect Library instance created as new Library("User32.dll"); or new ActiveXObject("Shell.Application")?

But you leaved main theme this issue - I be glad to help you with JS refactoring. P.S. look at yourself before teaching someone - I didn't use some coding/doc style because you do don't have it - read about .gitbub/ISSUE_TEMPLATE

sieukrem commented 4 years ago

OK let us discuss!

I mean, debugger can inspect Library instance created as new Library("User32.dll"); or new ActiveXObject("Shell.Application")?

Yes, the debugger allows to inspect and modify properties of instances.

But you leaved main theme this issue - I be glad to help you with JS refactoring.

Maybe because I did not understand the content of the issue description. Let me summarize what I understand from the description.

P.S. look at yourself before teaching someone - I didn't use some coding/doc style because you do don't have it - read about .gitbub/ISSUE_TEMPLATE

Nobody is perfect. ;-) Please don't judge, english is my third language. It was never my intention to teach you or to be unpolite. It was a try to shorten the mail ping pong by giving you the opportunity to do the best by you self if I let you know what are quality properties.

You are right there is no issue template or contribution rules. It was not werth to setup them if I was alone. Rules are very important as soon as there are more contributors. They prevent misunderstandings.

WinterSilence commented 4 years ago

@sieukrem

I assume you meant console.log could not print out string representation of complex objects like array or object

console.log()

The code of class Console is not about console.log or codemirror, but about Terminal

nope, my example mixed browser console and cli terminal as in NppExec

codemirror - readable "shugar" for dumping values

WinterSilence commented 4 years ago

@sieukrem

If I understand it correctly, then we could create 2 feature requests. One for Terminal and another for Console.

class with one method(Terminal) look like strange +that naming it's confuse users. as you say, when project grow up then code grow up too. I vote to solutions based on OOP, but focused on easy/smarty scripting and created for np++ users - juniors started learn js, it's similar to greacemonkey.

sieukrem commented 4 years ago

From the perspective of design principles (e.g Interface Segregation Principle it is fully OK to have two abstractions instead of one. OOP is only a tool to satisfy such design principles by implementing several design patterns.

But back to the features you want to implement.

Terminal - the window at the bottom of application allowing to input commands, execute them asyncrously to the n++ and printing the stderr and stdout of the program appending it at the end. Terminal is used primary by end user of n++, who is writing or reading text or shell scrips.

Console.log() - the possibility for javascript developer to print context relevant information from every place in code which he wants to investigate. It provides a simple debugging possibility. This tool is only important at the development time of javascripts for n++. The browser way to printout the result of conlsole.log() is the Developer Window. In the Nodejs it is the stdout or stderr.

There is no need to implement the terminal and console in one class.

WinterSilence commented 4 years ago

@sieukrem

Terminal - the window at the bottom of application allowing to input commands, execute them asyncrously to the n++ and printing the stderr and stdout of the program appending it at the end.

no, no I'm talk about debug console without emulation terminal. I want easy tools for handy scripting without external tools as VS. Something similar to Greasemonkey, try create script in it - light editor with hightlight code and linter + some helpers to fast coding.

sieukrem commented 2 years ago

The conversation stopped!