Skip to content

Commit

Permalink
Merge branch 'feature/#9_AlignCommands'
Browse files Browse the repository at this point in the history
  • Loading branch information
takumisoft68 committed Oct 27, 2020
2 parents 9a16252 + 5f269dc commit 7c5fef3
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 19 deletions.
12 changes: 10 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@

All notable changes to the "markdowntable" extension will be documented in this file.

### - 0.3.0 (2020/10/27)

- [Add] Align column/columns commands
- Align column to left/center/right.
- ![align](images/align_column.gif)
- Align selected multi columns at once.
- ![align](images/align_columns_at_once.gif)

### - 0.2.2 (2020/9/5)

- Fix
- [Fix]
- Tab key confliction with accepting suggestion or snippet.

### - 0.2.1 (2020/9/2)

- Fix bugs of navigate to next/prev cell
- [Fix] bugs of navigate to next/prev cell
- Bahavior when Tab key pressing out of table.
- Bahavior when Shift+Tab key pressing out of table.

Expand Down
40 changes: 28 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,19 @@ Markdown table features.

## 1. Features

- Navigate to next/prev cell
- Convert to table from TSV text
- Format all tables
- Insert column in the right/left
| Title | Command | Default Keybinding | In the Editor Right Click Menu |
| :------------------------- | :------------------------ | :----------------- | :---------------------------------- |
| Format all tables. | markdowntable.format | Shift + Alt + F | Yes |
| Convert TSV to table. | markdowntable.tsvToTable | Shift + Alt + T | Yes (only when selecting range) |
| Navigate to next cell. | markdowntable.nextCell | Tab | |
| Navigate to previous cell. | markdowntable.prevCell | Shift + Tab | |
| Insert row in the right. | markdowntable.insertRight | | Yes (only when not selecting range) |
| Insert row in the left. | markdowntable.insertLeft | | Yes (only when not selecting range) |
| Align to Left. | markdowntable.alignLeft | | Yes |
| Align to Center. | markdowntable.alignCenter | | Yes |
| Align to Right. | markdowntable.alignRight | | Yes |

---

## 2. Demo

Expand All @@ -21,7 +30,7 @@ Key binding to `Tab`.
- with auto format
- ![navigate](images/navigate_next_cell.gif)

### 2.1. Navigate to prev cell
### 2.2. Navigate to prev cell

Key binding to `Shift`+`Tab`.

Expand All @@ -30,21 +39,29 @@ Key binding to `Shift`+`Tab`.
- with auto format
- ![navigate_prev](images/navigate_prev_cell.gif)

### 2.2. Convert to table from TSV text
### 2.3. Convert to table from TSV text

Key binding to `Shift + Alt + T`.

- **Tips: This feature is supposed to make table from excel cells.**
- ![convert](images/table_from_excel.gif)

### 2.3. Format table
### 2.4. Align column/columns

- **Align column to left/center/right.**
- ![align](images/align_column.gif)

- **Align selected multi columns at once.**
- ![align](images/align_columns_at_once.gif)

### 2.5. Format table

Key binding to `Shift + Alt + F`.

- **Auto format column width of all tables in current document**
- ![formattable](images/format_table.gif)

### 2.4. Insert column
### 2.6. Insert column

- Add context menu to insert column
- ![insert](images/insert.gif)
Expand All @@ -66,12 +83,11 @@ What's focused on.

## 5. Release Notes

### - 0.2.2 (2020/9/5)
### - 0.3.0 (2020/10/27)

- Fix
- Tab key confliction with accepting suggestion or snippet.
- [Add] Align column/columns commands

## 7. Links
## 6. Links

- [Source Code](https://github.com/takumisoft68/vscode-markdown-table)
- [Marketplace](https://marketplace.visualstudio.com/items?itemName=TakumiI.markdowntable)
Binary file added images/align_column.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/align_columns_at_once.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 35 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"name": "markdowntable",
"publisher": "TakumiI",
"displayName": "Markdown Table",
"description": "A minimal extension for markdown table. Add context menu to edit markdown table.",
"version": "0.2.2",
"description": "A minimal extension for markdown table. Add features to edit markdown table.",
"version": "0.3.0",
"repository": {
"type": "git",
"url": "https://github.com/takumisoft68/vscode-markdown-table"
Expand All @@ -20,7 +20,10 @@
"onCommand:markdowntable.insertRight",
"onCommand:markdowntable.insertLeft",
"onCommand:markdowntable.nextCell",
"onCommand:markdowntable.prevCell"
"onCommand:markdowntable.prevCell",
"onCommand:markdowntable.alignLeft",
"onCommand:markdowntable.alignCenter",
"onCommand:markdowntable.alignRight"
],
"main": "./out/extension.js",
"contributes": {
Expand All @@ -43,11 +46,23 @@
},
{
"command": "markdowntable.nextCell",
"title": "Markdown Table: Move cursor into next cell."
"title": "Markdown Table: Navigate to next cell."
},
{
"command": "markdowntable.prevCell",
"title": "Markdown Table: Move cursor into previous cell."
"title": "Markdown Table: Navigate to previous cell."
},
{
"command": "markdowntable.alignLeft",
"title": "Markdown Table: Align to Left."
},
{
"command": "markdowntable.alignCenter",
"title": "Markdown Table: Align to Center."
},
{
"command": "markdowntable.alignRight",
"title": "Markdown Table: Align to Right."
}
],
"keybindings": [
Expand Down Expand Up @@ -93,6 +108,21 @@
"when": "editorLangId == markdown && !editorHasSelection",
"command": "markdowntable.insertLeft",
"group": "markdowntable@4"
},
{
"when": "editorLangId == markdown",
"command": "markdowntable.alignLeft",
"group": "markdowntable@5"
},
{
"when": "editorLangId == markdown",
"command": "markdowntable.alignCenter",
"group": "markdowntable@6"
},
{
"when": "editorLangId == markdown",
"command": "markdowntable.alignRight",
"group": "markdowntable@7"
}
]
}
Expand Down
125 changes: 125 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,131 @@ export function activate(context: vscode.ExtensionContext) {
// The code you place here will be executed every time your command is executed
insertColumn(true);
});


let alignColumns = (alignMark: [string, string]) => {
// エディタ取得
const editor = vscode.window.activeTextEditor as vscode.TextEditor;
// ドキュメント取得
const doc = editor.document;
// 選択範囲取得
const cur_selection = editor.selection;
// 選択範囲の始まり行
const currentLine = doc.getText(new vscode.Selection(
new vscode.Position(cur_selection.start.line, 0),
new vscode.Position(cur_selection.start.line, 10000)));
// テーブル内ではなかったら終了
if (!currentLine.trim().startsWith('|')) {
vscode.window.showErrorMessage('Markdown Table : Align command failed, because your selection is not starting from inside of a table.');
return;
}

// 表を探す
let startLine = cur_selection.start.line;
let endLine = cur_selection.start.line;
while (startLine - 1 >= 0) {
const line_selection = new vscode.Selection(
new vscode.Position(startLine - 1, 0),
new vscode.Position(startLine - 1, 10000));

const line_text = doc.getText(line_selection);
if (!line_text.trim().startsWith('|')) {
break;
}
startLine--;
}
while (endLine + 1 < doc.lineCount) {
const line_selection = new vscode.Selection(
new vscode.Position(endLine + 1, 0),
new vscode.Position(endLine + 1, 10000));

const line_text = doc.getText(line_selection);
if (!line_text.trim().startsWith('|')) {
break;
}
endLine++;
}
if (endLine < cur_selection.end.line) {
vscode.window.showErrorMessage('Markdown Table : Align command failed, because your selection is hanging out of the table.');
return;
}
const table_selection = new vscode.Selection(
new vscode.Position(startLine, 0),
new vscode.Position(endLine, 10000));
const table_text = doc.getText(table_selection);

// テーブルの変形処理クラス
const mdt = new markdowntable.MarkdownTable();

// 選択セルを取得
const [startline, startcharacter] = [cur_selection.start.line - startLine, cur_selection.start.character];
const [startRow, startColumn] = mdt.getCellAtPosition(table_text, startline, startcharacter);
const [endline, endcharacter] = [cur_selection.end.line - startLine, cur_selection.end.character];
const [endRow, endColumn] = mdt.getCellAtPosition(table_text, endline, endcharacter);

// テーブルをTableDataにシリアライズ
let tableData = mdt.stringToTableData(table_text);
if (tableData.aligns[0][0] === undefined) {
return;
}

// 選択範囲の列のAlignを変更する
if (startRow === endRow) {
// 選択範囲の開始位置と終了位置が同じ行内の場合
for (let column = startColumn; column <= endColumn; column++) {
tableData.aligns[column] = alignMark;
}
}
else if (startRow + 1 === endRow) {
// 選択範囲が2行にまたがる場合
for (let column = startColumn; column <= tableData.columns.length; column++) {
tableData.aligns[column] = alignMark;
}
for (let column = 0; column <= endColumn; column++) {
tableData.aligns[column] = alignMark;
}
}
else {
// 選択範囲が3行以上にまたがる場合はすべての列が対象
for (let column = 0; column < tableData.columns.length; column++) {
tableData.aligns[column] = alignMark;
}
}

// テーブルをフォーマット
const newTableText = mdt.tableDataToFormatTableStr(tableData);

//エディタ選択範囲にテキストを反映
editor.edit(edit => {
edit.replace(table_selection, newTableText);
});

// 元のカーソル選択位置を計算
const [anchorline, anchorcharacter] = [cur_selection.anchor.line - startLine, cur_selection.anchor.character];
const [anchorRow, anchorColumn] = mdt.getCellAtPosition(table_text, anchorline, anchorcharacter);

// 新しいカーソル位置をフォーマット後のテキストから計算
const [newline, newcharacter] = mdt.getPositionOfCell(newTableText, anchorRow, anchorColumn);
const newPosition = new vscode.Position(
table_selection.start.line + newline,
table_selection.start.character + newcharacter + 1);
const newSelection = new vscode.Selection(newPosition, newPosition);

// カーソル位置を移動
editor.selection = newSelection;
};

registerCommandNice('markdowntable.alignLeft', () => {
alignColumns([':', '-']);
});

registerCommandNice('markdowntable.alignCenter', () => {
alignColumns([':', ':']);
});

registerCommandNice('markdowntable.alignRight', () => {
alignColumns(['-', ':']);
});
}

// this method is called when your extension is deactivated
Expand Down

0 comments on commit 7c5fef3

Please sign in to comment.