Skip to content

Commit

Permalink
Merge branch 'feature/#10_NavigateCommandWithoutFormat'
Browse files Browse the repository at this point in the history
  • Loading branch information
takumisoft68 committed Nov 6, 2020
2 parents feb670d + c709273 commit 3dbc302
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 40 deletions.
38 changes: 25 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,25 @@ Markdown table features.

## 1. Features

| 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 column to the right.| markdowntable.insertRight | | Yes (only when not selecting range) |
| Insert column to 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 |
| 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 | No |
| Navigate to previous cell. | markdowntable.prevCell | Shift + Tab | No |
| Navigate to next cell (w/o format). | markdowntable.nextCellWithoutFormat | | No |
| Navigate to previous cell (w/o format). | markdowntable.prevCellWithoutFormat | | No |
| Insert column in the right. | markdowntable.insertRight | | Yes (only when not selecting range) |
| Insert column 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

### 2.1. Navigate to next cell (with auto insert row)
### 2.1. Navigate to next cell (with auto format & auto insert row)

Key binding to `Tab`.

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

#### 2.1.1 Without auto format

- Use markdowntable.nextCellWithoutFormat command
- If you want, you need to assign this command to some key binding by yourself.

### 2.2. Navigate to prev cell

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

#### 2.2.1 Without auto format

- Use markdowntable.prevCellWithoutFormat command
- If you want, you need to assign this command to some key binding by yourself.

### 2.3. Convert to table from TSV text

Key binding to `Shift + Alt + T`.
Expand Down Expand Up @@ -83,7 +95,7 @@ What's focused on.

## 5. Release Notes

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

- [Add] Align column/columns commands

Expand Down
Binary file modified images/insert.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 11 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"publisher": "TakumiI",
"displayName": "Markdown Table",
"description": "A minimal extension for markdown table. Add features to edit markdown table.",
"version": "0.3.1",
"version": "0.4.0",
"repository": {
"type": "git",
"url": "https://github.com/takumisoft68/vscode-markdown-table"
Expand All @@ -21,6 +21,8 @@
"onCommand:markdowntable.insertLeft",
"onCommand:markdowntable.nextCell",
"onCommand:markdowntable.prevCell",
"onCommand:markdowntable.nextCellWithoutFormat",
"onCommand:markdowntable.prevCellWithoutFormat",
"onCommand:markdowntable.alignLeft",
"onCommand:markdowntable.alignCenter",
"onCommand:markdowntable.alignRight"
Expand Down Expand Up @@ -52,6 +54,14 @@
"command": "markdowntable.prevCell",
"title": "Markdown Table: Navigate to previous cell."
},
{
"command": "markdowntable.nextCellWithoutFormat",
"title": "Markdown Table: Navigate to next cell (without auto format)."
},
{
"command": "markdowntable.prevCellWithoutFormat",
"title": "Markdown Table: Navigate to previous cell (without auto format)."
},
{
"command": "markdowntable.alignLeft",
"title": "Markdown Table: Align to Left."
Expand Down
92 changes: 66 additions & 26 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,20 @@ export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(command);
}

registerCommandNice('markdowntable.nextCell', (args) => {
let navigateNextCell = (withFormat :boolean) => {
// エディタ取得
const editor = vscode.window.activeTextEditor as vscode.TextEditor;
// ドキュメント取得
const doc = editor.document;
// 選択範囲取得
const cur_selection = editor.selection;
// カーソル行
const currentLine = doc.getText(new vscode.Selection(
const currentLine = new vscode.Selection(
new vscode.Position(cur_selection.active.line, 0),
new vscode.Position(cur_selection.active.line, 10000)));
new vscode.Position(cur_selection.active.line, 10000));
const currentLineText = doc.getText(currentLine);
// テーブル内ではなかったら終了
if (!currentLine.trim().startsWith('|')) {
if (!currentLineText.trim().startsWith('|')) {
// 通常のインデント
vscode.commands.executeCommand('tab');
return;
Expand Down Expand Up @@ -66,8 +67,7 @@ export function activate(context: vscode.ExtensionContext) {
const table_selection = new vscode.Selection(
new vscode.Position(startLine, 0),
new vscode.Position(endLine, 10000));
const table_text = doc.getText(table_selection);

let table_text = doc.getText(table_selection);


// テーブルの変形処理クラス
Expand All @@ -92,34 +92,50 @@ export function activate(context: vscode.ExtensionContext) {
(isNextRow && prevRow >= tableData.cells.length + 1)
);

// 次の行が必要なら追加する
if (isInsertNewRow === true) {
tableData = mdt.insertRow(tableData, tableData.cells.length);
if (withFormat) {
// 次の行が必要なら追加する
if (isInsertNewRow === true) {
tableData = mdt.insertRow(tableData, tableData.cells.length);
}

// テーブルをフォーマット
table_text = mdt.tableDataToFormatTableStr(tableData);
}
else {
// 次の行が必要なら追加する
if (isInsertNewRow === true) {
table_text += '\n' + tableData.indent + '|' + ' |'.repeat(tableData.columns.length);
}

// テーブルをフォーマット
const newTableText = mdt.tableDataToFormatTableStr(tableData);
// | が足りていないときは追加する
if (currentLineText.split('|').length < tableData.columns.length + 2) {
let table_text_lines = table_text.split(/\r\n|\n|\r/);
const cursorRow = cur_selection.active.line - startLine;
table_text_lines[cursorRow] += '|';
table_text = table_text_lines.join('\r\n');
}
}

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

// 新しいカーソル位置を計算
// character の +1 は表セル内の|とデータの間の半角スペース分
const newColumn = (isNextRow === true) ? 0 : prevColumn + 1;
const newRow = (isNextRow === true) ? prevRow + 1 : prevRow;
const [newline, newcharacter] = mdt.getPositionOfCell(newTableText, newRow, newColumn);
const [newline, newcharacter] = mdt.getPositionOfCell(table_text, newRow, newColumn);
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.prevCell', (args) => {
let navigatePrevCell = (withFormat :boolean) => {
// エディタ取得
const editor = vscode.window.activeTextEditor as vscode.TextEditor;
// ドキュメント取得
Expand Down Expand Up @@ -165,7 +181,7 @@ export function activate(context: vscode.ExtensionContext) {
const table_selection = new vscode.Selection(
new vscode.Position(startLine, 0),
new vscode.Position(endLine, 10000));
const table_text = doc.getText(table_selection);
let table_text = doc.getText(table_selection);



Expand All @@ -187,26 +203,50 @@ export function activate(context: vscode.ExtensionContext) {
return;
}

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

//エディタ選択範囲にテキストを反映
editor.edit(edit => {
edit.replace(table_selection, newTableText);
});
if (withFormat) {
// テーブルをフォーマット
table_text = mdt.tableDataToFormatTableStr(tableData);
//エディタ選択範囲にテキストを反映
editor.edit(edit => {
edit.replace(table_selection, table_text);
});
}

// 新しいカーソル位置を計算
// character の +1 は表セル内の|とデータの間の半角スペース分
const newColumn = (prevColumn > 0) ? prevColumn - 1 : tableData.columns.length - 1;
const newRow = (prevColumn > 0) ? prevRow : prevRow -1;
const [newline, newcharacter] = mdt.getPositionOfCell(newTableText, newRow, newColumn);
const newPosition = new vscode.Position(
const [newline, newcharacter] = mdt.getPositionOfCell(table_text, newRow, newColumn);
let newPosition = new vscode.Position(
table_selection.start.line + newline,
table_selection.start.character + newcharacter + 1);
table_selection.start.character + newcharacter);
if( doc.getText(new vscode.Selection(
table_selection.start.line + newline, table_selection.start.character + newcharacter,
table_selection.start.line + newline, table_selection.start.character + newcharacter + 1)) === ' ') {
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.nextCell', (args) => {
navigateNextCell(true);
});

registerCommandNice('markdowntable.prevCell', (args) => {
navigatePrevCell(true);
});

registerCommandNice('markdowntable.nextCellWithoutFormat', (args) => {
navigateNextCell(false);
});

registerCommandNice('markdowntable.prevCellWithoutFormat', (args) => {
navigatePrevCell(false);
});

registerCommandNice('markdowntable.tsvToTable', () => {
Expand Down

0 comments on commit 3dbc302

Please sign in to comment.