Skip to content

Commit

Permalink
[M] utils function
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrnperl committed Oct 10, 2023
1 parent 275fc70 commit 45d41cc
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 45 deletions.
29 changes: 0 additions & 29 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,32 +253,3 @@ export interface InsertOp extends Op {
nodes: SymbolNode[];
}

// eslint-disable-next-line @typescript-eslint/ban-types
export function debounce(func: Function, delay: number){
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let timer: any = null;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
return function(this: any, ...args: any[]){
if (timer){
clearTimeout(timer);
}
timer = setTimeout(() => {
func.apply(this, args);
}, delay);
};
}

// eslint-disable-next-line @typescript-eslint/ban-types
export function throttle(func: Function, limit: number){
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let inThrottle: any = false;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return function(this: any, ...args: any[]){
if (!inThrottle){
func.apply(this, args);
inThrottle = true;
setTimeout(() => inThrottle = false, limit);
}
};
}
3 changes: 2 additions & 1 deletion src/extension/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import {
import { OutlineView } from './outline';

import { commandList } from './commands';
import { debounce, ScrollMsg, throttle } from '../common';
import { ScrollMsg } from '../common';
import { config } from './config';
import { debounce, throttle } from '../utils';

// called when extension is activated
// extension is activated the very first time the command is executed
Expand Down
43 changes: 43 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

export const SymbolKindList = [
'File', 'Module', 'Namespace', 'Package', 'Class', 'Method',
'Property', 'Field', 'Constructor', 'Enum', 'Interface',
'Function', 'Variable', 'Constant', 'String', 'Number',
'Boolean', 'Array', 'Object', 'Key', 'Null', 'EnumMember',
'Struct', 'Event', 'Operator', 'TypeParameter'
];

// eslint-disable-next-line @typescript-eslint/ban-types
export function debounce(func: Function, delay: number){
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let timer: any = null;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
return function(this: any, ...args: any[]){
if (timer){
clearTimeout(timer);
}
timer = setTimeout(() => {
func.apply(this, args);
}, delay);
};
}

// eslint-disable-next-line @typescript-eslint/ban-types
export function throttle(func: Function, limit: number){
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let inThrottle: any = false;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return function(this: any, ...args: any[]){
if (!inThrottle){
func.apply(this, args);
inThrottle = true;
setTimeout(() => inThrottle = false, limit);
}
};
}

// Camel case to dash case
export function camelToDash(str: string): string {
return str.replace(/([a-zA-Z])(?=[A-Z])/g, '$1-').toLowerCase();
}
4 changes: 2 additions & 2 deletions src/webview/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { OverlayScrollbars } from 'overlayscrollbars';
import './main.scss';
import { Input } from './input';
import '@vscode/codicons/dist/codicon.css';
import { camelToDash } from '../utils';

/**
* The root element of the outline
Expand Down Expand Up @@ -196,8 +197,7 @@ function renderSymbolNode(symbolNode: SymbolNode, depth = 0): HTMLDivElement {
container.dataset.range = JSON.stringify(symbolNode.range);
container.classList.toggle('leaf', symbolNode.children.length === 0);
container.style.setProperty('--depth', depth.toString());
let iconName = symbolNode.kind.toLowerCase();
iconName = iconName === 'enummember' ? 'enum-member' : iconName;
const iconName = camelToDash(symbolNode.kind);
container.innerHTML = /*html*/`
<div class="outline-label">
<span class="expand-btn codicon codicon-chevron-right"></span>
Expand Down
14 changes: 1 addition & 13 deletions src/webview/input.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { SwitchButton } from './components/switchButton';
import { InputArea } from './components/inputArea';
import { throttle } from '../utils';

customElements.define('switch-button', SwitchButton);
customElements.define('input-area', InputArea);
Expand Down Expand Up @@ -419,16 +420,3 @@ class Searcher {
}
}

// eslint-disable-next-line @typescript-eslint/ban-types
function throttle(func: Function, limit: number){
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let inThrottle: any = false;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return function(this: any, ...args: any[]){
if (!inThrottle){
func.apply(this, args);
inThrottle = true;
setTimeout(() => inThrottle = false, limit);
}
};
}

0 comments on commit 45d41cc

Please sign in to comment.