Skip to content

Commit

Permalink
Document formatting provider added (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
pmahend1 authored Sep 9, 2021
1 parent 778cb81 commit 52887a0
Show file tree
Hide file tree
Showing 13 changed files with 784 additions and 776 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Change Log

## Stable

### 1.0.0 : 9-Sep-2021

- Document formatting provider added.🆕

## Preview

### 0.11.0 : 8-Aug-2021
Expand Down
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Pretty XML

<img src='./images/logo.png' width='200' height='200' />
<img src='./images/logo.png' width='200' height='200' />

> 🆕 What's new in 1.0.0
> Now you can set Pretty XML as your default XML formatter.
> If you have multiple formatters installed then right click and select 'Format Document with...' and choose 'Configure Default Formatter' to change it.
> Once done you can use formatting keybind to format , default is usually **Shift + Alt + F**
> You can still use Pretty XML command key Ctrl + K L too.
Pretty XML is a XML formatter extension for Visual Studio Code and VSCodium. It formats XML documents just like Visual Studio on Windows.
**Supported file extensions**: xml, xaml, xsd, xsl, plist, mobileconfig, config, csproj, axml, resx and all other XML type of files.
Expand Down Expand Up @@ -33,7 +39,7 @@ Right Click and Select Prettify XML or use [shortcut](#keyboard-shortcuts)
- No empty lines.
- Supports `'` and whitespace unicodes in attribute value for XAML parser compatibility.

_before_
_before_

![Before](./images/before.png)

Expand Down
14 changes: 9 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
},
"readme": "https://github.com/pmahend1/PrettyXML/blob/master/README.md",
"description": "XML formatter extension for Visual Studio Code. Formats XML documents just like Visual Studio.",
"version": "0.11.0",
"version": "1.0.0",
"publisher": "PrateekMahendrakar",
"repository": {
"url": "https://github.com/pmahend1/prettyxml.git"
},
"icon": "images/logo.png",
"license": "MIT",
"bugs": "https://github.com/pmahend1/prettyxml/issues",
"preview": true,
"preview": false,
"galleryBanner": {
"color": "#445678",
"theme": "dark"
Expand Down Expand Up @@ -170,8 +170,8 @@
"@types/node": "^16.4.13",
"@types/vscode": "^1.59.0",
"@typescript-eslint/eslint-plugin": "^4.29.0",
"@typescript-eslint/parser": "4.29.0",
"electron-rebuild": "^2.3.5",
"@typescript-eslint/parser": "4.31.0",
"electron-rebuild": "^3.2.3",
"eslint": "^7.6.0",
"glob": "^7.1.6",
"mocha": "^9.0.2",
Expand All @@ -180,8 +180,12 @@
"vscode-test": "^1.4.0"
},
"dependencies": {
"@types/compare-versions": "^3.3.0",
"compare-versions": "^3.6.0",
"electron-edge-js": "file:./lib/electron-edge-js",
"electron-edge-js-mac": "file:./lib/electron-edge-js-mac",
"install": "^0.13.0"
"fs": "^0.0.1-security",
"install": "^0.13.0",
"path": "^0.12.7"
}
}
16 changes: 16 additions & 0 deletions src/documentFilter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as vscode from "vscode";
export class DocumentFilter implements vscode.DocumentFilter
{
language?: string;

scheme?: string;

pattern?: vscode.GlobPattern;

constructor(language?: string, scheme?: string, pattern?: vscode.GlobPattern)
{
this.language = language ?? undefined;
this.scheme = scheme ?? undefined;
this.pattern = pattern ?? undefined;
}
}
77 changes: 77 additions & 0 deletions src/documentHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/* eslint-disable @typescript-eslint/naming-convention */
// imports
import * as vscode from "vscode";
import { DocumentFilter } from "./documentFilter";
export class DocumentHelper
{
constructor()
{

}


public static get Editor()
{
return vscode.window.activeTextEditor;
}

//get Range of the active document
public static getEditorRange(): vscode.Range
{
if (this.Editor)
{
//get editor text
let document = this.Editor.document;
var start = new vscode.Position(0, 0);
var lastButOne = document.lineAt(document.lineCount - 1);
var end = new vscode.Position(document.lineCount, lastButOne.range.end.character);
var ranger = new vscode.Range(start, end);
return ranger;
}
else
{
return new vscode.Range(0, 0, 0, 0);
}
}

public static getDocumentText(): string
{
if (this.Editor)
{
return this.Editor.document.getText();
}
else
{
throw new Error("Editor not found!");
}
}

public static replaceTextForRange(range: vscode.Range, newText: string)
{
if (this.Editor)
{
this.Editor.edit(editBuilder =>
{
editBuilder.replace(range, newText);
});
}
}

public static replaceDocumentText(newText: string)
{
if (this.Editor)
{
var range = this.getEditorRange();
this.Editor.edit(editBuilder =>
{
editBuilder.replace(range, newText);
});
}
}

public static createLanguageDocumentFilters(language : string): vscode.DocumentFilter[]
{
var filters = [ new DocumentFilter(language, 'file'), new DocumentFilter(language, 'untitled') ];
return filters;
}
}
Loading

0 comments on commit 52887a0

Please sign in to comment.